FOR Loop – Print from 1 to 5

Below is a program to print numbers from 1 to 5. This is a basic example of for loop in QBASIC. The main objective is to understand what loop is and how it is used.

' Program to print numbers from 1 to 5
CLS
FOR i = 1 TO 5
  PRINT i
NEXT i
END

Explanation:

Let’s first understand what a loop in QBASIC is. Loop means to run a piece of code a repeated number of times. In the above program FOR loop is used following which is i = 1 TO 5. Here, i is a loop variable whose value changes from 1 to 5. But why 1 to 5? We are writing a program to print the numbers from 1 to 5 so the value of i starts from 1 and ends at 5. The start and end values of the loop variable are dependent upon the requirement of the program.

Inside the loop is a PRINT statement that is displaying the value of loop variable i.

NEXT is used to increment the value of the loop variable.

Leave a Comment

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