Beginner's Programming Tutorial in QBasic

(Kiana) #1

Loops


"Loops" make it easier to do an action multiple times. There are at least four types of loops:
IF...GOTO, WHILE...WEND, DO...LOOP, and FOR...NEXT.


IF...GOTO


This program uses IF...GOTO to create a loop:
x = 10
start:PRINT x
x = x + 1 (This adds 1 to x)
IF x < 15 THEN GOTO start
Output:
10


(^1112)
13
14


WHILE...WEND


The WHILE...WEND commands continue a loop until a specified expression is false.
To use WHILE...WEND:



  1. Place an expression after WHILE

  2. Enter a list of commands

  3. Place WEND at the end


Run the following:
x = 10
WHILE x < 15
PRINT x
x = x + 1
WEND

Free download pdf