Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Skipping within a For Loop
#1
Logically the answer to my question here is NO but embarrassment from asking stupid coding questions is my Forte.

When you have a loop control range, can you skip a specific step within the range? So for example the For Loop is 

For x = 50 to -50
   if x = 0 then next
Next

or if there may be a couple within the controlled range is there a way to

For x = 50 to -50
     if x = 10 or x = 0 or x = -10 then Next
Next

Thanks
Reply
#2
There are not stupid questions, only stupid questioners. Big Grin

Anyway, kidding aside...

Code: (Select All)
FOR i = 10 TO -10 STEP -1
    IF i <> 0 THEN
        PRINT i
    END IF
NEXT

That's how I would code for your first example. There may be others who would do so, differently.

For the second example...

Code: (Select All)
REM Use your mouse wheel to scroll the console output.
$CONSOLE:ONLY
FOR i = 50 TO -50 STEP -1
    IF ABS(i) = 10 OR i = 0 THEN
    ELSE
        PRINT i
    END IF
NEXT

Just thought I'd have some fun and hand you an empty THEN statement. I use this technique sometimes when I rather write the condition one way more than the opposite way. IF ABS(i) <> 10 and ABS(i) <> 0 in this case. The ELSE part wouldn't be needed if I wrote it that way.

The STEP part is a neat way to control the loop. NEGATIVE 1 allowed use to get the - to + output in these examples. You can also skip using STEP -2, STEP 5, STEP -10, etc. STEP can be used with positive numbers to. FOR i = 0 TO 10 STEP 2 would give us... 0, 2, 4, 6, 8, 10. If we used that exclude zero again, we'd get 2, 4, 6, 8, 10 in the output.

Pete
Reply
#3
_CONTINUE is what you're looking for.

_CONTINUE - QB64 Phoenix Edition Wiki
Reply
#4
For x = 50 to -50
if x = 0 then _continue
Next

For x = 50 to -50
if x = 10 or x = 0 or x = -10 then _continue
Next
Reply
#5
You could use the _CONTINUE keyword to skip the remainder of the loop.

Code: (Select All)
For x = 50 To -50 Step -1
    If x = 10 Or x = 0 Or x = -10 Then _Continue

    Print x;
Next

Edit: Steve, you beat me to it. XD
Reply
#6
(10-23-2022, 03:54 PM)a740g Wrote: You could use the _CONTINUE keyword to skip the remainder of the loop.

Code: (Select All)
For x = 50 To -50 Step -1
    If x = 10 Or x = 0 Or x = -10 Then _Continue

    Print x;
Next

Edit: Steve, you beat me to it. XD

Dang!  If someone had just told me about that nice looking command!  Tongue
Reply
#7
That's a new keyword for me, too.

I still like using conditional statements better, and I never resorted to GOTO or THEN as GOTO methods...

Code: (Select All)
FOR i = 1 TO 10
    IF i = 5 THEN 100
    100
NEXT

' OR...

FOR i = 1 to 10
if i = 5 THEN GOTO bypass
bypass:
NEXT

One new keyword I hope QB64 doesn't develop is POP. POP was available in languages like Atari BASIC, and maybe T.I., as a way to exit a GOSUB without a RETURN. It simply cleared the stack. I think POP screwed a lot of beginner programmers for using other languages and winding up with stack space errors.

Pete
Reply
#8
That _Continue is really great. Checked the wiki and Select Case seemed to be missing so I tried it

Print
Print
For x = 1 To 10
    Select Case x
        Case 1
            Print x;
        Case 2
            _Continue
        Case 3
            Print x;
        Case 4
            Print x;
        Case 5
            _Continue
        Case 6
            Print x;
        Case 7
            Print x;
        Case 8
            _Continue
        Case 9
            Print x;
        Case 10
            Print x;
    End Select
Next


And it works in Select Case as well. And now that it has been brought to my attention, I think I may have either asked this question before or was reading something similar at the old site. I think I do have an old routine where I used _Continue in a Select Case algorythm. Anyway appreciate your help once again.
Reply
#9
Well, this is a CASE where _CONTINUE isn't even needed. Also, if you want to make the cases simplified, try...

Code: (Select All)
PRINT
PRINT
FOR x = 1 TO 10
    SELECT CASE x
        CASE 1, 3, 4, 6, 7, 9, 10
            PRINT x;
    END SELECT
NEXT

Now all examples will do the same thing, but if you start to get into 100's or 1000's of lines of code, keep in mind smaller routines make for easier understanding and debugging.

Pete
Reply
#10
It's not SELECT CASE that's being skipped, but rather the FOR...NEXT.

_CONTINUE will work anywhere in a loop as far as I know, but just be careful where you place it. Since it short circuits the loop it is in, it will skip all subsequent code that occurs after it until the NEXT/LOOP/WEND. You just have to make sure you're not doing something important in the loop after the _CONTINUE.

Code: (Select All)
PRINT
PRINT
FOR x = 1 TO 10
    SELECT CASE x
        CASE 2
            _CONTINUE
        CASE 8
            _CONTINUE
    END SELECT
    PRINT x;
NEXT
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
Reply




Users browsing this thread: 22 Guest(s)