Looping in Qbasic

Codynn
3 Min Read

A loop is a set of instructions that is repeated until a certain result is achieved. Loops are used to run a group of instructions repeatedly until a certain condition is satisfied. The QBasic programming language supports the following sorts of loops to fulfill looping needs:

A loop is a series of instructions that are repeated until the desired result is obtained. Loops are used to run a set of instructions again and over until a condition is met. The QBasic programming language supports the following sorts of loops to fulfill looping needs:

● FOR…NEXT loop

The FOR…NEXT statement is used to run a series of statements a specified number of times. When you know precisely how many times you need to run the loop, it’s usually utilized as a counter loop.

CLS
Fact = 1
INPUT " Enter a number "; n
FOR j = 1 TO n
Fact = Fact * j
NEXT j
PRINT “Factorial is “; Fact
END

WHILE…WEND loop:

When a condition is true, the WHILE statement executes a block of statements continually. If the condition is true, the WHILE statement executes the body of the loop, and the loop ends when the condition is false.

Example:
CLS
INPUT "enter any number "; num
n = num
sum = 0
WHILE num <> 0
r = num MOD 10
num = num \ 10
sum = sum + (r ^ 3)
WEND
IF n = sum THEN
PRINT "It is Armstrong "
ELSE
PRINT "It is not Armstrong "
END IF
END

Do While loop:

A do-while loop is a control flow statement that executes a block of code at least once before continuing or interrupting it based on a Boolean condition at the block’s end.

CLS
c = 1
DO WHILE c <= 10
    PRINT c
    c = c + 1
LOOP
END

Please download our app by clicking the image below:

Download Qbasic application on your phone
Download Qbasic application on your phone

If the above link is not working. You can use this link –> https://play.google.com/store/apps/details?id=com.allbachelor.qbasicapp

Share This Article
Leave a comment

Leave a Reply

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