In FOR..NEXT jumping to next iteration.

General FreeBASIC programming questions.
Post Reply
davidshq
Posts: 52
Joined: Oct 29, 2008 3:28
Contact:

In FOR..NEXT jumping to next iteration.

Post by davidshq »

Say I have a FOR..NEXT something like this:

FOR i = 1 to 10
NEXT

And inside of it I have a few IF..THEN statements like so:

FOR i = 1 to 10
IF [condition] THEN

Code: Select all

    END IF
    IF [condition2] THEN
      [code2]
    END IF
NEXT

Now, lets say that condition is true and code is executed, but once the code is executed I don't want it to run the second IF..THEN, instead I immediately want to jump to the next iteration of i - is this possible? In pseudo-code:

FOR i = 1 to 10
    IF [condition] THEN
      NEXT i
    END IF
    IF [condition2] THEN
      [code2]
    END IF
NEXT
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: In FOR..NEXT jumping to next iteration.

Post by TJF »

... and Wiki ElseIF
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: In FOR..NEXT jumping to next iteration.

Post by MrSwiss »

This is not directly related to your question. But seems to me to be important:

Instead of using multiple IF ... ELSEIF ... ELSEIF ... ELSE ... ENDIF statements,
(probably nested xy times), use:

ONE! ... Select Case ... statement for condition-evaluation and branching.
More info on above, ist in the REFERENCE documentation of FBC.

This is important to have READABLE CODE, as well as good programming style.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: In FOR..NEXT jumping to next iteration.

Post by TJF »

@MrSwiss:

You're right, SELECT [AS CONST] ... CASE ... is faster and better readable than multiple IF ... ELSEIF ... statements. But you cannot check for multiple conditions (like a = 1, b = "2", c >=3, ...). Instead the same variable is checked against multiple values (like a = 1, a = 2, a >= 3, ...).
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: In FOR..NEXT jumping to next iteration.

Post by MrSwiss »

@TJF,

you are correct for (like a = 1, b = 2, c >= 3, ...), but this is in many cases done inside SELECT CASE ... like:

Code: Select all

for i as Long = 0 to x step 5 
  Select Case As Const i
  Case 0                           ' only on first run (of FOR ... NEXT loop)
    If a < 0 or a > 99 then          ' Bounds checking, if a = index (of array)
      ...                            ' raise ERROR or fix a
    else
      ...                            ' normal operation
    endif
  Case 5                           ' only on second run
    ...
  Case Else                        ' default option (all remaining cycles)
    ...
  End Select
  ...
  ...
next i    ' BTW i is optional
bcohio2001
Posts: 556
Joined: Mar 10, 2007 15:44
Location: Ohio, USA
Contact:

Re: In FOR..NEXT jumping to next iteration.

Post by bcohio2001 »

Maybe if you posted the code that you have, all can look at it and give you better way.
Theunis Jansen
Posts: 248
Joined: Jul 01, 2010 9:35

Re: In FOR..NEXT jumping to next iteration.

Post by Theunis Jansen »

An old dirty code - leaving most programmer shouting no no. but it is the same as JMP. and is judicious use of GOTO
Give your Next I a label; viz
CheckI:
Next I

If an IF THEN decision is true directly before END IF say - GOTO CheckI

Of course this will skip to the next iteration but it will start checking from the first IF THEN again and if it remains true none of the other decisions will be checked until the loop ends.
If you wish to Exit the FOR....NEXT loop when an IF....THEN is true without checking anything else then before each END IF you can say EXIT FOR.
(Before EXIT FOR was introduced we used to say i = the maximum plus one)

So the question is what is the purpose not to check the Next IF... THEN
Post Reply