The process of repeating or doing same task many times until the given condition is true is called looping or iteration. There are different looping statements are used in QBASIC such as FOR … NEXT, WHILE …. WEND, DO … LOOP, etc. It allows a specified group of statements to be executed a certain number of times while certain condition is true. Among these looping statements FOR … NEXT is the most common and popular looping statement.
FOR … NEXT Loop
The FOR … Next is a most popular and mostly used looping statement which is used to execute the set of statements repeatedly for a given number of times.
Syntax:
FOR = TO STEP n
[statements]
NEXT
Example 1
WAP to print the natural numbers from 1 to 1.
CLS FOR x = 1 TO 10 PRINT x; NEXT x END
Example 2
WAP to print even numbers from 1 to 20
CLS FOR i = 2 TO 20 STEP 2 PRINT i; NEXT i END
Example 3
WAP to print the numbers from 20 to 1 in descending order.
CLS FOR a = 20 TO 1 STEP -1 PRINT a; NEXT a END
Example 4
WAP to print your name 10 times
CLS FOR i = 1 TO 10 PRINT "Your Name" NEXT i END
Example 5
WAP to print the multiplication table of given number.
CLS INPUT "Enter a number "; n FOR c = 1 TO 10 PRINT n; "x"; c; "="; n * c NEXT c END
WHILE …. WEND
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.
Syntax:
WHILE
[statements]
WEND
Example 1
WAP to print the natural numbers from 1 to 10
CLS n = 1 WHILE n <= 10 PRINT n; n = n + 1 WEND END
Example 2
WAP to print even numbers from 1 to 20
CLS n = 2 WHILE n <= 20 PRINT n; n = n + 2 WEND END
Example 3
WAP to print the numbers from 20 to 1 in descending order.
CLS n = 20 WHILE n >= 1 PRINT n; n = n - 1 WEND END
Example 4
WAP to print your name 10 times
CLS n = 1 WHILE n <= 10 PRINT "Your Name" n = n + 1 WEND END
Example 5
WAP to print the multiplication table of given number.
CLS i = 1 INPUT "Enter any number "; n WHILE i <= 10 PRINT n; "x"; i; "="; i * n i = i + 1 WEND END
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 given below.
Syntax 1 | Syntax 2 |
---|---|
DO … WHILE [statements] LOOP |
DO [statements] LOOP WHILE |
Syntax 3 | Syntax 4 |
DO … UNTIL [statement] LOOP |
DO [statement] LOOP UNTIL |
While writing a program we can fallow any one syntax among them.
Example 1
WAP to print the natural numbers from 1 to 10
CLS CLS n = 1 DO PRINT n; n = n + 1 LOOP WHILE n <= 10 END
Example 2
WAP to print even numbers from 1 to 20
CLS n = 2 DO PRINT n; n = n + 2 LOOP WHILE n <= 20 END
Example 3
WAP to print the numbers from 20 to 1 in descending order.
CLS n = 20 DO PRINT n; n = n - 1 LOOP UNTIL n < 1 END
Example 4
WAP to print your name 10 times
CLS n = 1 DO WHILE n <= 10 PRINT "Your Name" n = n + 1 LOOP END
Example 5
WAP to print the multiplication table of given number.
CLS i = 1 INPUT "Enter any number "; n DO WHILE i <= 10 PRINT n; "x"; i; "="; i * n i = i + 1 LOOP END
NESTED LOOP
A nested loop is a loop within a loop, an inner loop within the body of an outer one. This repeats until the outer loop finishes. QBASIC allows to use one loop inside another loop. To understand nested loop following section shows a few examples.
Example 1
CLS FOR i = 1 TO 4 PRINT "This is an outer loop "; i FOR j = 1 TO 3 PRINT "Inner Loop "; j NEXT j PRINT NEXT i END
Example 2
CLS FOR i = 1 TO 5 FOR j = 1 TO i PRINT j; NEXT j PRINT NEXT i END
Example 3
CLS FOR i = 1 TO 5 FOR j = 1 TO i PRINT i; NEXT j PRINT NEXT i END
Example 4
CLS FOR i = 5 TO 1 STEP -1 FOR j = 1 TO i PRINT j; NEXT j PRINT NEXT i END
Example 5
CLS FOR i = 5 TO 1 STEP -1 FOR j = 1 TO i PRINT i; NEXT j PRINT NEXT i END
<< Table of Content >>