Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Skipping within a For Loop
#21
Sure there's a difference. _CONTINUE is like a special needs GOTO. It needs a loop. GOTO can go anywhere. Big Grin

Anyway. To your point about _CONTINUE and error trapping. It doesn't need a line number. Here are two examples in one snippet. The second one addresses the error trapping question. Just press a key after the first demo is done.

Code: (Select All)
ON ERROR GOTO resnxt
FOR i = 10 TO -10 STEP -1
    IF i MOD 2 THEN _CONTINUE
    LOCATE , ABS(i): PRINT i ' Errors when i = 0.
NEXT

SLEEP

LOCATE 15, 10
FOR i = 10 TO -10 STEP -1
    IF SCREEN(CSRLIN, POS(0) - (10 - ABS(i))) = 32 THEN _CONTINUE
    PRINT "It will not go here. because _CONTINUE skips it even after error."
NEXT
END

resnxt:
BEEP
PRINT "Error"; ERR, "i ="; i,
RESUME NEXT

Good question!

Pete
Shoot first and shoot people who ask questions, later.
Reply
#22
I realize this is off topic Pete but, if we are heading towards the elimination of GOTO, wouldn't it be nice if we could just code ON ERROR resnxt (or whatever the label is carrying the coding handling the error. Maybe if its a label then perhaps it should read ON ERROR :resnxt.  A topic for another thread.
Reply
#23
(10-24-2022, 06:03 PM)Dimster Wrote: ... if we are heading towards the elimination of GOTO ...

What the hell? Are there plans on eliminating GOTO from QB64PE ?
Reply
#24
(10-24-2022, 06:19 PM)CharlieJV Wrote:
(10-24-2022, 06:03 PM)Dimster Wrote: ... if we are heading towards the elimination of GOTO ...

What the hell?  Are there plans on eliminating GOTO from QB64PE ?

No, definitely not. GOTO is a culprit of "Pasta" code but it can also be a better way to code in rare instances. I have a couple of large programs where the use of one GOTO statement saves me from using a lot of conditional statements and sometimes, when a program is being modified, too much re-tooling instead of using one GOTO could screw up part of your program. So then you spend hours to fix something that one GOTO fixes in 10-seconds. (I type slow.)

Pete
Reply
#25
LOL don't say "GOTO" is going to be taken away from a BASIC dialect, to a certain book author who insisted on the keyword because he didn't like "DO... LOOP" for silly reasons. He was compelled to use "WHILE... WEND". The book was about "systems" programming with M$QuickBASIC v4.0.

If "GOTO" is taken away from BASIC it would have to be taken away from C as well. All of it, including the functionality C didn't have in the 1980's involving pointers, which opens possibilities for self-modifying code. (Oh no I already said it, am I going to be banned?)

(10-24-2022, 06:03 PM)Dimster Wrote: I realize this is off topic Pete but, if we are heading towards the elimination of GOTO, wouldn't it be nice if we could just code ON ERROR resnxt (or whatever the label is carrying the coding handling the error. Maybe if its a label then perhaps it should read ON ERROR :resnxt.  A topic for another thread.
Take away the colon then "resnxt" should be a subprogram. Not a "FUNCTION".
Reply
#26
(10-24-2022, 03:52 PM)Dimster Wrote: Don't you think there is a big difference between _Continue and GOTO? 
You and many others using QB64PE should be glad to have that choice. In Freebasic absolutely cannot use "GOTO" to jump out of blocks if there are local variables involved; in that dialect could have local variables even inside "IF... THEN... END IF" block. I don't know if it acts that way in "-lang qb" mode. As I've already said their "CONTINUE" implementation means a bit more typing, allows to jump out of more than one nested loop but must keep track of them in order from inner to outer. Could have something like "CONTINUE FOR, DO, FOR, WHILE" LOL which looks dumb to me.
Reply
#27
Loop jumping Pete style...

Stupid demo, but you'll get the point.
Code: (Select All)
CLS
INPUT "Input 1 to jump one loop, 2 to jump 2, 3 to jump all: "; x
WHILE -1
    DO
        FOR i = -5 TO 5
            IF i = x THEN
                SELECT CASE x
                    CASE 1
                        EXIT FOR
                    CASE 2
                        EXIT DO
                    CASE 3
                        EXIT WHILE
                    CASE ELSE
                        PRINT "oops."
                        END
                END SELECT
            END IF
        NEXT
        IF x = 1 AND nextloop = 0 THEN PRINT "Next loop was exited. Press any key to redo.": nextloop = -1
        IF LEN(INKEY$) THEN RUN
    LOOP
    IF doloop = 0 THEN PRINT "Do loop was exited. Press any key to redo": doloop = -1
    IF LEN(INKEY$) THEN RUN
WEND
PRINT "While loop was exited. Press any key to redo"
SLEEP
RUN

Anyway, it's a neat trick to enclose a DO/LOOP inside a WHILE/WEND.

Pete
Reply




Users browsing this thread: 6 Guest(s)