BCA 1st Year QBASIC Looping Statement Prakash Dayal
BCA 1st Year QBASIC Looping Statement Prakash Dayal
In QBASIC
loop is a sequence of instructions that is repeated until
a certain condition is reached.
Example
FOR i = 1 TO 4 step 2
PRINT “Hello world!"
NEXT i
Write a program to print the natural numbers from 1 to 10.
Cls
FOR x=1 to 10
Print x
Next x
End
CLS
FOR I = 2 to 20 step 2
PRINT I
NEXT I
END
WHILE ... WEND loop
In a WHILE ... WEND loop, if the condition is True, all statements are executed until WEND
keyword is encountered. If the condition is false, the loop is exited and the control jumps to
very next statement after WEND keyword.
Example
i=10
While i>5
Print “hello”
i=i-1
Wend
DO ... LOOP
It is another type of looping statement in QBASIC. Sometime it is also called
DO WHILE LOOP. In this loop, the instructions within the loop are performed if
the comparison is true. We can use this loop by 4 different methods. Syntax
are
Do loop’s Syntax
Syntex 1
Syntex 2
DO … WHILE
DO
{STATEMENTS}
{STATEMENTS}
LOOP
LOOP WHILE [CONDITION]
Syntex 3
Syntex 4
DO UNTIL
{STATEMENTS} DO
LOOP {STATEMENTS}
LOOP UNTIL
Example
CLS
n=1
DO
PRINT n;
n=n+1
Loop While n<=10
End
CLS
n=20
DO
PRINT n;
n= n- 1
LOOP UNTIL n<1
END
END