Skipping within a For Loop
#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


Messages In This Thread
Skipping within a For Loop - by Dimster - 10-23-2022, 03:38 PM
RE: Skipping within a For Loop - by Pete - 10-23-2022, 03:46 PM
RE: Skipping within a For Loop - by SMcNeill - 10-23-2022, 03:49 PM
RE: Skipping within a For Loop - by SMcNeill - 10-23-2022, 03:52 PM
RE: Skipping within a For Loop - by PhilOfPerth - 10-23-2022, 11:03 PM
RE: Skipping within a For Loop - by a740g - 10-23-2022, 03:54 PM
RE: Skipping within a For Loop - by SMcNeill - 10-23-2022, 03:56 PM
RE: Skipping within a For Loop - by Pete - 10-23-2022, 04:15 PM
RE: Skipping within a For Loop - by Dimster - 10-23-2022, 04:48 PM
RE: Skipping within a For Loop - by Pete - 10-23-2022, 04:58 PM
RE: Skipping within a For Loop - by OldMoses - 10-23-2022, 06:08 PM
RE: Skipping within a For Loop - by Pete - 10-23-2022, 08:30 PM
RE: Skipping within a For Loop - by Dimster - 10-23-2022, 10:02 PM
RE: Skipping within a For Loop - by Pete - 10-23-2022, 11:36 PM
RE: Skipping within a For Loop - by OldMoses - 10-23-2022, 11:48 PM
RE: Skipping within a For Loop - by Pete - 10-24-2022, 12:11 AM
RE: Skipping within a For Loop - by mnrvovrfc - 10-24-2022, 11:18 AM
RE: Skipping within a For Loop - by Dimster - 10-24-2022, 03:52 PM
RE: Skipping within a For Loop - by mnrvovrfc - 10-24-2022, 07:16 PM
RE: Skipping within a For Loop - by Pete - 10-24-2022, 04:46 PM
RE: Skipping within a For Loop - by Dimster - 10-24-2022, 06:03 PM
RE: Skipping within a For Loop - by CharlieJV - 10-24-2022, 06:19 PM
RE: Skipping within a For Loop - by Pete - 10-24-2022, 06:27 PM
RE: Skipping within a For Loop - by mnrvovrfc - 10-24-2022, 07:11 PM
RE: Skipping within a For Loop - by Pete - 10-24-2022, 08:44 PM



Users browsing this thread: 13 Guest(s)