Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
neerajthakur
Creator III
Creator III

Use While Loop and For Loop to generate odd/even numbers of given range (0-100) or n integers.

I would like to generate Odd/Even Numbers using While / For  Loop separately for cases:

1. Range is given 0-100

2. For n integers

Please guide me on how I can do this using both While and For Loop

#qliksense

Thanks & Regards,
Please Accepts as Solution if it solves your query.
1 Solution

Accepted Solutions
Vegar
MVP
MVP

The script sample below can help you get started. 

//Loop that handles both odd and even numbers 
FOR _i = 0 to 100 step 1
	LOAD 
		$(_i) as Number,
		if(odd($(_i)),'Odd', 'Even') as NumberType,
		IF(odd($(_i)),$(_i)) as OddNumber,
		IF(even($(_i)),$(_i)) as EvenNumber
	AutoGenerate 1;
NEXT

//Loop for fetching only even numbers
For _even = 0 to 100 step 2
	LOAD $(_even) as EvenNumber
	AutoGenerate 1;
NEXT

//Loop for fetching only even numbers
For _odd = 1 to 100 step 2
	LOAD $(_odd) as OddNumber
	AutoGenerate 1;
NEXT

View solution in original post

4 Replies
Vegar
MVP
MVP

The script sample below can help you get started. 

//Loop that handles both odd and even numbers 
FOR _i = 0 to 100 step 1
	LOAD 
		$(_i) as Number,
		if(odd($(_i)),'Odd', 'Even') as NumberType,
		IF(odd($(_i)),$(_i)) as OddNumber,
		IF(even($(_i)),$(_i)) as EvenNumber
	AutoGenerate 1;
NEXT

//Loop for fetching only even numbers
For _even = 0 to 100 step 2
	LOAD $(_even) as EvenNumber
	AutoGenerate 1;
NEXT

//Loop for fetching only even numbers
For _odd = 1 to 100 step 2
	LOAD $(_odd) as OddNumber
	AutoGenerate 1;
NEXT
neerajthakur
Creator III
Creator III
Author

Here is what I wrote using DO While

//Odd Nos.
Set a=1;

Do while a<100
Trace $(a);
Let a=a+2;
Loop

Thanks & Regards,
Please Accepts as Solution if it solves your query.
Vegar
MVP
MVP

@neerajthakur  Your do while... should do the trick. Do you run into issues with it? Please explain.

neerajthakur
Creator III
Creator III
Author

Nope it worked fine, initially had some syntax issues but I understood them and corrected it. Now its working as expected.

Thanks & Regards,
Please Accepts as Solution if it solves your query.