QBASIC DO…LOOP WHILE – Print numbers from 1 to 5

Following is a basic program to print numbers from 1 to 5 using DO…LOOP WHILE loop in QBASIC.

' Program to print numbers from 1 to 5
CLS
i = 1
DO 
  PRINT i
  i = i + 1
LOOP WHILE i <= 5
END

Explanation:

DO…LOOP WHILE is similar to WHILE…WEND loop except for the piece of code is executed first before the condition is checked. Let’s understand the syntax and differences from WHILE…WEND loop. The loop starts with initializing the loop variable followed by a DO statement following a print statement and incrementing the value of the loop variable.

After incrementing the value of the loop variable, the LOOP WHILE statement is used to check the condition to terminate the loop and if the condition is satisfied, the cursor is sent back to DO statement, else terminates the loop.

Leave a Comment

Your email address will not be published. Required fields are marked *