# Qbasic Looping statements with Examples

Qbasic is an easy-to-learn programming language, especially for beginners. It provides a wide range of features and functionalities that can make learning a programming language fun and enjoyable. And one of the features that is important in any programming language is the use of looping statements.

In this article, we will understand one of the control statements i.e the looping statement in QBASIC programming. You will learn about different looping statements with it’s syntax and examples.

## What is a Looping Statement in QBASIC?

In any programming language loop is an important concept. It is a process where we have to repeat a task multiple times until the given condition becomes true. And to perform the loop operation QBASIC provides us with different looping statements like FOR..NEXT, DO LOOP, WHILE WEND, etc.

So by definition, a looping statement in QBASIC is a statement that helps us to loop or repeat a task multiple times until the given condition is `True`. Using a looping statement we can execute a statement (or a group of statements) many times until it fulfills a given condition.

There are four types of looping statements in QBasic:

Let’s understand it better with some examples:

## FOR NEXT Looping statement

The `FOR NEXT` loop is used to repeat a group of statements for a specific number of times. It uses a counter variable to keep track of the number of times the loop has been executed.

A counter is a variable that counts the iteration of a loop in a program. It needs two values :

1. An initial value, and
2. An incremented value

Syntax:

``````FOR [counter] = [initial] TO [end value] STEP [stepvalue]
``````

[statements]

NEXT [counter] END

Example 1: Print the natural numbers from 1 to 10 in QBASIC

``````CLS
FOR num = 1 TO 10
PRINT num;
NEXT num
END``````

Example 2: Write a QBASIC program to find the sum of all even numbers from 1 to 20 (Using STEP)

``````CLS
sum = 0
FOR i = 2 TO 20 STEP 2
sum = sum + i
NEXT i
PRINT "Sum of even numbers from 1 to 20 ="; sum``````

## DO LOOP looping statement

The `DO..LOOP` statement repeats a group of statements/codes while a particular condition is True. It is generally preferred when the number of iterations is not known beforehand.

The DO…LOOP statements are of two types:

1. DO WHILE
2. DO UNTIL

### DO WHILE loop

The `DO WHILE` loop executes a code as long as the specified condition is True. The DO WHILE loop first executes the statement in the program and then checks the condition.

Syntax:

``````DO
``````

[statement]

LOOP WHILE [condition] END

Example:

``````CLS
n = 1
DO
PRINT n
n = n + 1
LOOP WHILE n <= 10
END``````

### DO UNTIL loop

The `DO UNTIL` loop repeats a group of code as long as the given condition is false.

Syntax:

``````DO UNTIL [condition]
``````

[statement]

LOOP END

Example:

``````CLS
count = 0
DO UNTIL count = 10
count = count + 1
PRINT "Count is now " + STR\$(count)
LOOP
END``````

## WHILE WEND looping statement

A `WHILE...WEND` loop statement runs all the statements as long as the condition is True. If the condition becomes False, the loop ends and the program continues with the statement after the WEND keyword.

Syntax:

``````CLS
WHILE [condition]
``````

[statement]

WEND END

Example:

``````CLS
n = 20
WHILE n < 25
PRINT n
n = n+1
WEND
END``````

## NESTED FOR…NEXT looping statement

When we use FOR NEXT loop inside another FOR NEXT loop statement, we call the statements the `NESTED FOR...NEXT` looping statement.

The outer FOR NEXT statement that engulfs the inner second FOR NEXT statement is called the Outer Loop.

And the second FOR NEXT statement that is inside the outer loop is called the Inner Loop.

Syntax:

``````FOR [outer variable] = starting value TO ending value
FOR [inner variable] = starting value TO ending value
[Statements to be executed]
NEXT [inner variable]
NEXT [outer variable]``````

Example:

``````FOR i = 1 TO 3 ' Outer Loop
FOR j = 1 TO 2 ' Inner Loop
PRINT "i = "; i; ", j = "; j
NEXT j
NEXT i``````

Conclusion:

In conclusion, we can say that looping statements are a fundamental part of learning a programming language. It helps us to reduce code and help the program run effectively.

In QBASIC, there are four main looping statements FOR NEXT, NESTED FOR NEXT, WHILE WEND, and DO LOOP. In this article on QBASIC looping statement, we have understood all four loop statements along with their syntax and examples.

Related Articles:

Free QBasic Online Compilers – Online Editors

QBASIC Programming – Beginner’s Friendly