The FOR ... NEXT Loop

Normally programs flow along line by line (linear execution) in the order in which the line appears in your source code.

The FOR - NEXT statements enable you to repeat a block of code a set number of times (if you know exactly how many times you need to go through the loop).

Here is what an FOR-NEXT loop structure  looks like in QBasic...

If you are uncertain about how many times you want to go through the loop, then you should use a WHILE loop or a DO loop.

Sample:

DIM x AS INTEGER
DIM y AS INTEGER
DIM z AS INTEGER
REM example #1
FOR  x = 1 to 10 
    y = x * 2
    COLOR x
    PRINT y
NEXT  x
REM example #2

z = 12
For  x = z  to  z+10 
    y = x * 2
    PRINT y
Next  x
PRINT "Good Bye"
 
A more Complex version of the FOR-NEXT Loop:

"More Advanced" For Loop

For    loop_counter = StartValue to EndValue Step stepValue
     statement;
     statement;
Next loop_counter

WHERE:

  • The For statement is the beginning of the loop
  • StartValue = the initial value of the loop counter
  • EndValue = the value that the loop counter must pass to exit the loop
  • The stepValue (Optional) is the amount counter is changed each time through the loop. If not specified, step defaults to one.
  • The step argument (stepValue) can be either
    • positive (counter increases)
    • negative (counter decreases)
    • Loop executes if
      • Positive: counter <= end
      • Negative: counter >= end
  • The Next statement is the end of the loop

After all statements in the loop have executed, step is added to counter. At this point, either the statements in the loop execute again (based on the same test that caused the loop to execute initially), or the loop is exited and execution continues with the statement following the Next statement.

Sample:

DIM a AS INTEGER
DIM b AS INTEGER
DIM c AS INTEGER
DIM x AS INTEGER
DIM y AS INTEGER
DIM z AS INTEGER
REM example #1

For    x = 10  to 100  Step  10 
    y = x * 2
    PRINT y
Next  x

REM example #2

For    x = 1000 to 100 Step -100 
     y = x * 2
     PRINT y
Next  x
REM example #3

a = 3
b = 11
c = 2
For    x = a  to b Step c 
    y = x * 2
    PRINT y
Next  x
PRINT "Good Bye"

* If a NEXT statement is encountered before its corresponding FOR statement, an error occurs.

 

How to avoid problems and errors using the FOR - NEXT loop:

Syntax Errors:
  • Spelling words the wrong way

Logic Errors:

  • Infinite Loops: this happens when the checking condition is ALWAYS true.
     
    • Example #1: The value of X will always be less than the ending value.
      This happens when you change the value of the  variable that is the loop counter inside the loop.

      For X = 1 to 10
          PRINT "The number is ";X
          X=2
      NEXT x
       
  • Loop is not repeated the correct number of times: this happens when you change the value of the  variable that is the loop counter inside the loop.
    • Example #1:

      For X = 1 to 10
          PRINT "The number is ";X
          X=X+2
      NEXT x
       
  • Not entering a loop:this happens when the starting value is larger than the ending value and you are not counting down (i.e. not using the STEP -1 option).
     

    • Example #1: The value of X starts out bigger than the ending value (10 >1)  so the loop will be skipped.

      For X = 10 to 1
          PRINT "The number is ";X
      NEXT x

      To fix this loop use the "STEP" option with this loop to make it count down instead of counting up...

      For X= 10 to 1 STEP -1
          PRINT "The number is ";X
      NEXT x