Skipping within a For Loop - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10) +---- Thread: Skipping within a For Loop (/showthread.php?tid=1001) |
Skipping within a For Loop - Dimster - 10-23-2022 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 RE: Skipping within a For Loop - Pete - 10-23-2022 There are not stupid questions, only stupid questioners. Anyway, kidding aside... Code: (Select All) FOR i = 10 TO -10 STEP -1 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. 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 RE: Skipping within a For Loop - SMcNeill - 10-23-2022 _CONTINUE is what you're looking for. _CONTINUE - QB64 Phoenix Edition Wiki RE: Skipping within a For Loop - SMcNeill - 10-23-2022 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 RE: Skipping within a For Loop - a740g - 10-23-2022 You could use the _CONTINUE keyword to skip the remainder of the loop. Code: (Select All) For x = 50 To -50 Step -1 Edit: Steve, you beat me to it. XD RE: Skipping within a For Loop - SMcNeill - 10-23-2022 (10-23-2022, 03:54 PM)a740g Wrote: You could use the _CONTINUE keyword to skip the remainder of the loop. Dang! If someone had just told me about that nice looking command! RE: Skipping within a For Loop - Pete - 10-23-2022 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 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 RE: Skipping within a For Loop - Dimster - 10-23-2022 That _Continue is really great. Checked the wiki and Select Case seemed to be missing so I tried it 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. RE: Skipping within a For Loop - Pete - 10-23-2022 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 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 RE: Skipping within a For Loop - OldMoses - 10-23-2022 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 |