Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Nested Loops - How to enforce a Next?

Hi,

I'm using 2 nested loops.

In the inner loop, I'm using IF blocks.

I would like to define a condition which would force the Next of the inner loop.

Using the Exit would cause performing commands in vetween the loops,

which is not desired.
I bypass it by using flags.But I wish to know whether it is possible to define something like:

IF condition THEN

     NEXT

ENDIF

To this end, didn't find a similar syntax.

Thanks 🙂

1 Solution

Accepted Solutions
jagan
Luminary Alumni
Luminary Alumni

Hi,

Try this,  if you don't give next it will automatically goto next for because u r else below it.  Remove next.

FOR I = 1 to $(vDelivNoOfRows)

// Here come a few commands

  FOR j =   $(Counter)+1 to (vReceiveNoOfRows)

//--Some commands

IF vBalance < 0 then
LET VDelQty = fabs($(vBalance));

SET vNextDelDoc = 'F';
LET Counter = $(Counter) + 1;
ELSEIF   vBalance = 0 then
LET Counter = $(Counter) + 1;
SET vNextDelDoc = 'T';
ELSE                              
LET VRecvQty = vBalance;                   
SET vNextDelDoc = 'T';
EXIT For;
ENDIF;

NEXT;

NEXT;


Regards,

Jagards.

View solution in original post

9 Replies
MK_QSL
MVP
MVP

can you load your script please?

Not applicable
Author

Hi Manish,

It looks something like that. The NEXT in red causes a script error:

FOR I = 1 to $(vDelivNoOfRows)

// Here come a few commands

  FOR j =   $(Counter)+1 to $(vReceiveNoOfRows)

//--Some commands

IF vBalance < 0 then
LET VDelQty = fabs($(vBalance));

SET vNextDelDoc = 'F';
LET Counter = $(Counter) + 1;
NEXT;                
ELSEIF   vBalance = 0 then
LET Counter = $(Counter) + 1;
SET vNextDelDoc = 'T';

ELSE
                             
LET VRecvQty = vBalance;                   
SET vNextDelDoc = 'T';
EXIT For;

ENDIF;


NEXT;

NEXT;

Thanks!

tresesco
MVP
MVP

May be this?



FOR I = 1 to $(vDelivNoOfRows)

// Here come a few commands

  FOR j =   $(Counter)+1 to $(vReceiveNoOfRows)

//--Some commands

IF vBalance < 0 then
LET VDelQty = fabs($(vBalance));

SET vNextDelDoc = 'F';
LET Counter = $(Counter) + 1;
NEXT;                
ELSEIF   vBalance = 0 then
LET Counter = $(Counter) + 1;
SET vNextDelDoc = 'T';

ELSE
                             
LET VRecvQty = vBalance;                   
SET vNextDelDoc = 'T';
EXIT For;

ENDIF;

END IF;


NEXT;

NEXT;

Not applicable
Author

Hi,

Sometimes, you want to move directly to the next record in the inner loop,

bypassing a group of commends between the two NEXT commands.

If it is not enabled in QlikView, the only way to do it is by using EXIT FOR and flags.

Thanks!

jagan
Luminary Alumni
Luminary Alumni

Hi,

Try this,  if you don't give next it will automatically goto next for because u r else below it.  Remove next.

FOR I = 1 to $(vDelivNoOfRows)

// Here come a few commands

  FOR j =   $(Counter)+1 to (vReceiveNoOfRows)

//--Some commands

IF vBalance < 0 then
LET VDelQty = fabs($(vBalance));

SET vNextDelDoc = 'F';
LET Counter = $(Counter) + 1;
ELSEIF   vBalance = 0 then
LET Counter = $(Counter) + 1;
SET vNextDelDoc = 'T';
ELSE                              
LET VRecvQty = vBalance;                   
SET vNextDelDoc = 'T';
EXIT For;
ENDIF;

NEXT;

NEXT;


Regards,

Jagards.

tresesco
MVP
MVP

Do you have any syntactical example from any other language to understand it better? I guess what you want can be achieved using IF..Else properly. Like:

For a=1 to 10

If a<>2 then                 // for a=2, the loop would move to next

     ...all others IFs.

End If

Next a

jagan
Luminary Alumni
Luminary Alumni

Hi,

Try Exit For When|Unless.

Regards,

Jagan.

Not applicable
Author

Got It!

Thanks!

Not applicable
Author

Well, I thought I recalled that it's possible in VB.

But It's not.

The right way to do it is like you and Jagan suggested.

Thanks!