FOR with Conditional IF...THEN NEXT Causes Problems (35718)



The information in this article applies to:

  • Microsoft QuickBASIC Compiler for the Apple Macintosh 1.0

This article was previously published under Q35718

SUMMARY

Executing an isolated NEXT statement within a block IF...END IF statement produces an error in the Macintosh QuickBASIC interpreter; however, it will compile and run.

Executing the NEXT statement without the matching FOR in the same IF statement is a bad programming practice because it may cause problems with the stack. This practice can cause the stack to overflow and generate a memory error, or cause the program's flow of control to branch incorrectly. (In the interpreter, the stack keeps track of where to return when a loop is finished.)

The NEXT statement should never be executed conditionally in a single-line or block IF...END IF statement. It is easy to find an alternative flow-of-control structure that performs the same logic.

MORE INFORMATION

The following are error messages you are likely to encounter in the interpreter when attempting to conditionally execute a NEXT statement.
IF without END IF
WHILE without WEND
FOR without Next
CASE without SELECT CASE
ELSE/ELSEIF/END IF without IF
The following is a code example that will give an "IF without END IF" error message in the interpreter, but will successfully compile and execute:
'    **** This is an example of BAD PROGRAMMING ****
FOR loopme = 1 to 2
    '    conditionally begin inner loop
    IF loopme = 1 THEN FOR i = 1 TO 5

    PRINT loopme; i; "Hi there."

    '    conditionally end inner loop
    IF loopme = 1 THEN    '(interpreter error here)
        NEXT i
    END IF
NEXT loopme
				
This example works in both environments if both IF statements are single-line IF statements. However, this unstructured programming practice may not be supported in future versions of Macintosh BASIC.

Modification Type:MinorLast Reviewed:1/9/2003
Keywords:KB35718