Loops: FOR, WHILE, DO
The condition is tested before each iteration.
10.1 FOR...NEXT
The limit and step are evaluated once at the start.
If the start is already past the limit, the body does not run at all.
After the loop, the counter holds the first value that failed the test (6 after FOR i = 1 TO 5).
Don't change the counter inside the loop.
FOR i = 1 TO 5
PRINT i;
NEXT i ' 1 2 3 4 5 (the variable after NEXT is optional)
FOR i = 10 TO 0 STEP -2 ' counting down
PRINT i;
NEXT ' 10 8 6 4 2 0
FOR x = 0 TO 1 STEP 0.25 ' fractional steps (beware of rounding)
PRINT x;
NEXT
FOR row = 1 TO 3 ' nested loops
FOR col = 1 TO 4
PRINT row * col;
NEXT col
PRINT
NEXT row10.2 WHILE...WEND
The condition is tested before each iteration. WHILE...WEND is the older form; DO WHILE...LOOP is more flexible.
n = 1
WHILE n < 1000
n = n * 2
WEND
PRINT n ' 102410.3 DO...LOOP — four flavors
' Test at the top (may run zero times)
DO WHILE balance > 0
balance = balance - 100
LOOP
DO UNTIL done
' ...
done = -1
LOOP
' Test at the bottom (always runs at least once)
DO
INPUT "Enter a positive number: ", n
LOOP UNTIL n > 0
DO
count = count + 1
LOOP WHILE count < 10
' Infinite loop with an explicit exit
DO
k$ = INKEY$
IF k$ = CHR$(27) THEN EXIT DO ' Esc key
_LIMIT 30 ' QB64: don't hog the CPU
LOOP10.4 EXIT and _CONTINUE
leave the innermost FOR loop leave the innermost DO loop EXIT SUB / EXIT FUNCTION return from the procedure _CONTINUE (QB64) skip to the next iteration of the current loop
' Find the first multiple of 7 greater than 50
FOR n = 51 TO 100
IF n MOD 7 = 0 THEN EXIT FOR
NEXT
PRINT "Found:"; n
' Print odd numbers only
FOR n = 1 TO 10
IF n MOD 2 = 0 THEN _CONTINUE
PRINT n;
NEXT10.5 Floating-point loop pitfall
Classic BASIC: FOR...NEXT and GOTO were the only loops in most 8-bit BASICs; some (Commodore, Applesoft) always run a FOR body at least once. BBC BASIC added REPEAT...UNTIL. FreeBASIC: For...Next, While...Wend, Do...Loop identical; Continue For / Continue Do / Exit For / Exit Do. VB.NET: For i As Integer = 1 To 10 ... Next, For Each item In list ... Next, Do While ... Loop, While ... End While (not Wend), Exit For, Continue For.
FOR x = 0 TO 1 STEP 0.1
PRINT x ' may stop at 0.9 because 0.1 is not exact in binary
NEXT
' Safer: loop over integers and compute the value
FOR i = 0 TO 10
x = i / 10
PRINT x
NEXT🧪 Part I Exercises
Write a program that asks for your name and birth year, then prints a greeting and your age at the end of this year.
Type in the classic line-numbered guessing game from 5.3, run it in QB64-PE, then rewrite it in structured style with a DO...LOOP and no line numbers.
Declare one variable of each type (INTEGER, LONG, _INTEGER64, SINGLE, DOUBLE, STRING, _UNSIGNED _BYTE); assign values near their limits and observe what happens one step beyond.
Write a temperature converter that reads Celsius and prints Fahrenheit and Kelvin with PRINT USING to two decimal places.
Predict, then verify, the output of: PRINT 17 \ 5, 17 MOD 5, -17 MOD 5, 2 ^ 3 ^ 2, NOT 0, 12 AND 10, 12 XOR 10.
Write a grade calculator using SELECT CASE with ranges (CASE 90 TO 100, CASE IS < 60).
Print a centered, colored title box on the text screen using LOCATE, COLOR, and STRING$.
Print a multiplication table from 1×1 to 12×12 aligned with PRINT USING.
Write a program that reads numbers until the user enters 0, then prints the count, sum, minimum, maximum, and average.
Print all prime numbers below 200 using nested FOR loops and EXIT FOR.