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
DO ... WHILE 
   [statements] 
LOOP
Syntax 2
DO 
  [statements] 
LOOP WHILE
Syntax 3
DO ... UNTIL 
  [statement]   
LOOP
Syntax 4
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

  4575