Contents
CBSE Notes for Class 7 Computer in Action – Looping Statements in QBASIC
QBASIC is a high-level programming language. In the previous class, you wrote simple programs in QBASIC with the help of statements such as PRINT, REM, CLS, LET, INPUT and IF…THEN… ELSE. Let us quickly revise these statements.
- The PRINT statement is used to display numbers, messages or values of variables on the output screen.
- The REM statement is used to write a remark or a comment in the program that does not get
- The CLS statement is used to clear the output screen.
- The LET statement is used to assign a value to a variable.
- The INPUT statement is used to take a value from the user and store it in a variable.
- The IF…THEN…ELSE is a conditional statement that executes a set of statements depending upon a condition.
INTRODUCTION TO LOOPS
While programming, you may want to repeat the execution of a certain set of statements multiple times. This can be done using loops. A loop is used to repeat the execution of a group of statements many times. Statements that enable us to create loops are known as looping statements. We will learn the following looping statements in QBASIC.
- FOR….. NEXT
- DO…..LOOP
Using FOR…NEXT Statement for Looping
The FOR…NEXT statement is used when you want a group of statements to be executed a specific number of times.
The general form of the FOR…NEXT statement is given below.
FOR counter = initial value to final value [STEP value] The loop body comprising a set of statements to be executed NEXT counter
The statements written between FOR and NEXT form the body of the loop.
In the above-mentioned FOR…NEXT statement,
- counter is a numeric variable that controls the loop and hence is also called the control or the index variable. Its value changes every time the loop body gets executed.
- step value is the value by which the counter variable is incremented or decremented every time the loop body is executed. It can be a positive or a negative value, but it cannot be zero. The step value is optional. In case it is omitted, the counter variable is increased by one every time the loop is executed.
Consider the following example based on FOR…NEXT loop.
In this example,
Control variable: A
Initial value of control variable: 1
Final value of control variable: 3
Step value: 1
Thus, this loop runs three times. The string “Hello” appears three times on the output screen (Fig. 5.1). When the loop ends, the statements written after the loop are executed. Consider the following example.
The output of this program is:
Using the step value
You can give a step value to specify the value by which the control variable should change. In the following program, the control variable X changes by 2 every time the body of the loop gets executed.
FOR X = 1 to 10 STEP 2 PRINT X NEXT X
The output of this program is:
You can also assign a negative number as a step value. In this case, the value of the control variable is decreased by the step value every time the loop is executed. When the step value is negative, the initial value of the control variable is more than the final value.
FOR X = 10 to 1 STEP -2
PRINT X
NEXT X
The output of this program is:
The step value can also be a real number.
FOR I = 1 TO 2 STEP 0.25 PRINT I NEXT I
The output of this program is:
Let us consider some more examples of FOR…NEXT loop.
Example 1: To add natural numbers from 1 to 20.
REM To add numbers from 1 to 20 CLS Sum=0 For I = 1 to 20 Sum = Sum + I NEXT I PRINT "Sum of numbers from 1 to 20 =" ; sum
The output of this program is:
Example 2: To add all even numbers from 1 to 20.
REM To find the sum of all even numbers from 1 to 20 CLS Sum=0 For I = 2 to 2 0 STEP 2 Sum = sum + I NEXT I Print "Sum of even numbers from 1 to 20 ="; sum
The output of this program is:
Example 3: To display the table of a number entered by the user.
REM To display the table of a number entered by the user PRINT "Enter a number" INPUT A For I = 1 to 10 PRINT A; "x"; I; "="; A*I NEXT I
The output of this program is:
Using DO…LOOP/or Looping
You may not always know the number of times a loop has to be executed. The execution of the statements may depend upon a condition. In such a situation, the use of a DO…LOOP is preferred over a FOR…NEXT loop.
There are two variations of the DO…LOOP.
- DO WHILE…LOOP
- DO UNTIL…LOOP
DO WHILE..LOOP
The DO WHILE…LOOP is executed as long as the specified condition is TRUE. The general form of the DO WHILE…LOOP is:
DO WHILE condition
The loop body comprising a set of statements to be executed
LOOP
The statements written between DO WHILE and LOOP form the body of the loop.
Let us look at an example of this loop.
A=1 DO WHILE A<3 PRINT A A=A+1 LOOP
The above loop is executed as long as the value of A is less than 3.
If the condition in the DO WHILE…LOOP is not true, the loop is not executed at all.
Consider the following example.
X=10 DO WHILE X<10 PRINT X LOOP
In this case, condition X<10 is not true and hence the loop body is not executed.
Let us look at a few more examples of the DO WHILE…LOOP.
Example 1: To display odd numbers less than 30.
REM To display odd numbers less than 30 CLS X=1 DO WHILE X<=30 PRINT X X=X+2 LOOP
The output of this program is:
Example 2: To add numbers entered by the user as long as the user wants to.
REM To add numbers entered by user CLS A$="Y" sum=0 DO WHILE A$="Y" INPUT "ENTER A NUMBER"; NUM sum=sum+num; INPUT "ENTER Y to add more numbers "; A$ LOOP PRINT "The sum of numbers = "; sum
The output of this program is :
DO UNTIL….LOOP
The DO UNTIL…LOOP is similar to the DO WHILE…LOOP. The only difference between the two loops is that in the DO UNTIL…LOOP, the execution of the loop continues as long as the condition is false.
The general form of the DO UNTIL…LOOP is:
DO UNTIL condition
The loop comprising a set of statements to be executed
LOOP
Let us look at an example of this loop.
A=1 DO UNTIL A>3 PRINT A A=A+1 LOOP PRINT "LOOP Over"
The output of this program is:
The above loop is executed as long as the condition A>3 is false or as long as A is less than or equal to 3.
Let us write the same program using the two types of DO…LOOP.
The two programs give the same output:
The condition in the DO…LOOP can also be placed at the end of the loop. In this case, the working of the loop remains the same except for one difference. When the condition is at the end of the loop, the loop is executed at least once even if the condition is false.
EXITING A LOOP
You can exit from a loop before the loop comes to an end on its own.
For this, you use the EXIT FOR statement in the FOR…NEXT loop and the EXIT DO statement in the DO WHILE…LOOP or the DO UNTIL…LOOP.
The following program finds the perimeter of 10 squares. The FOR…NEXT loop ends if the value of the side entered by the user is negative.
REM To find perimeter of 10 squares CLS FOR 1=1 to 10 PRINT "Enter the side of the square" INPUT side IF side<0 THEN EXIT FOR PRINT "Perimeter of Square = "; 4*side NEXT I PRINT "The Program Ends"
Words to Know
- Loop: It is used to repeat the execution of a group of statements many times.
- Looping statements: Statements that enable you to create loops.