Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Skipping within a For Loop
#11
Exactly. In these examples _CONTINUE only promotes a coding style. Without it, and still using just 2 and 8, we get...

Code: (Select All)
PRINT
PRINT
FOR x = 1 TO 10
    SELECT CASE x
        CASE 2, 8
        CASE ELSE
            PRINT x;
    END SELECT
NEXT
Shoot first and shoot people who ask questions, later.
Reply
#12
Good catch OldMoses.
Reply
#13
(10-23-2022, 03:52 PM)SMcNeill Wrote: 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
That's very elegant! Once again, a new (to me) function. I've never come across  _continue before, but it will become another tool in my arsenal (no, Pete, don't go there)!
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#14
And we aren't' supposed to just jump out of the ... oh i mean goto out of the loop because....?
Reply
#15
This is why I miss Bill. Who better to explain the physical properties of having a new tool in your arsenal?

In other news, I can't figure out what James is GETtng at.

Pete

- Sorry Phil, you know I had to go there after that set up...
Reply
#16
As Pete demonstrated, there are often quicker and more concise ways to skip a loop iteration. I use it fairly often, but it's hard to justify, except in rare circumstances. I agree, it's a style thing. It can make code a little harder to read for debugging too, since there's fewer indentation blocks over using an IF ... END IF block.

I like _CONTINUE for things like the following:

active% = 5
FOR x% = 1 to 10
    IF x% = active% THEN _CONTINUE
    'code to determine distance between objects x% and active%
NEXT x%

It tightens the line count (but not the typing) slightly from:

active% = 5
FOR x% = 1 to 10
    IF x% <> active% THEN
        'code to determine distance between objects x% and active%
    END IF
NEXT x%
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
Reply
#17
I know this is poor form but this makes me wonder why something like _continue exits:

Code: (Select All)
For i = 1 To 10
    If i <> 5 Then Print i; 'print i unless i =5
    On i GOTO do1, do2, do3 'keep the loop visually brief by going to other lines
    more:
Next
End
do1:
Print "!";
GoTo more
do2:
Print "#";
GoTo more
do3:
Print "@";
GoTo more

Next thing I know someone's going to show me the EXIT.
Reply
#18
I know. I can't say I'm a fan of _CONTINUE, either.

To the example James posted I'd say eat that pasta before it gets cold!

Of course this would be perfectly acceptable, right Steve?

Code: (Select All)
For i = 1 To 10
    If i <> 5 Then Print i; 'print i unless i =5
    On i GOSUB do1, do2, do3 'keep the loop visually brief by going to other lines
Next
End
do1:
Print "!";
RETURN
do2:
Print "#";
RETURN
do3:
Print "@";
RETURN

In the old days, before SELECT CASE, something like this would be an alternative to use IF/THEN/ELSE

I have to admit in some code of mine dated back to 2000 I've found the classic...

Code: (Select All)
FOR  I = 1 TO 10
IF I = 2 OR I = 8 THEN GOTO BYPASSI
PRINT I
BYPASSI:
NEXT

I coded all caps and no indentation or remarks, back then. Anyway, I wonder if the above example is the way the compiler translates this to the C/C++ output or if it encases the snippet into the C/C++ equivalent of:

Code: (Select All)
FOR  I = 1 to 10
IF I = 2 OR I = 8 THEN
ELSE
PRINT I
END IF
NEXT

Pete
Reply
#19
(10-23-2022, 03:38 PM)Dimster Wrote: :
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
I've seen something like this in somebody else's program which infuriated me. Even though it works and most BASIC interpreters accept it.

Using "NEXT" like that could cause outright confusion, for debugging the program many moons later or for somebody else who must look at the program, if the variable is never provided after "NEXT" and code is not commented. Try going three or more levels of "FOR... NEXT" nesting and using "NEXT" instead of "_CONTINUE". I guess the Freebasic developers dreaded that and that's why after "CONTINUE", in their product must specify which leading loop statement, just like with "EXIT". Also they have "EXIT SELECT" which I can't conceive a purpose. So much fuss only to have local variables inside an "IF... END IF" or "SELECT CASE... END SELECT" block.

Without "_CONTINUE" I found the example program in "DO... LOOP" in the Wiki very clever. Having "_CONTINUE" around makes programs more readable, however.

I have a weakness that sometimes I don't trust boolean evaluation even in Lua, which makes me write a condition that I want false so that something is executed. So often I'm doing something like:
Code: (Select All)
if a == 5 then
-- do nothing
else
-- do something important
end

In Lua could safely rewrite to "if not a == 5 then" or "if a ~= 5 then" but of course, in many program-scripts I wrote it's more complex than that. :/

(10-24-2022, 12:11 AM)Pete Wrote: :
I have to admit in some code of mine dated back to 2000 I've found the classic...

Code: (Select All)
FOR  I = 1 TO 10
IF I = 2 OR I = 8 THEN GOTO BYPASSI
PRINT I
BYPASSI:
NEXT
I have done the same thing, and I have even written a "preprocessor" for QB64 trying to fake "_CONTINUE" in that manner. It was difficult figuring out block "IF" statements which was the main reason why my attempt failed. :/
Reply
#20
Don't you think there is a big difference between _Continue and GOTO? 
The ability to exit just one LOOP or exit the entire LOOPing seems to me to be a lot different then sending the flow of the program all over the place with GOTO. 
In some ways _Continue reminds me of RETURN. I wonder if an Error Trapping routine would work with _Continue. Maybe it would need a line number to continue from.
Reply




Users browsing this thread: 3 Guest(s)