Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
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
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
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
Here is what I wrote using DO While
//Odd Nos.
Set a=1;
Do while a<100
Trace $(a);
Let a=a+2;
Loop
@neerajthakur Your do while... should do the trick. Do you run into issues with it? Please explain.
Nope it worked fine, initially had some syntax issues but I understood them and corrected it. Now its working as expected.