Microsoft KB Archive/35718

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 17:55, 18 July 2020 by 3155ffGd (talk | contribs) (importing KB archive)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Base


FOR with Conditional IF...THEN NEXT Causes Problems

Article ID: 35718

Article Last Modified on 1/9/2003



APPLIES TO

  • Microsoft QuickBasic Compiler for 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.


Additional query words: MQuickB

Keywords: KB35718