• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer

CBSE Tuts

CBSE Maths notes, CBSE physics notes, CBSE chemistry notes

  • NCERT Solutions
    • NCERT Solutions for Class 12 English Flamingo and Vistas
    • NCERT Solutions for Class 11 English
    • NCERT Solutions for Class 11 Hindi
    • NCERT Solutions for Class 12 Hindi
    • NCERT Books Free Download
  • TS Grewal
    • TS Grewal Class 12 Accountancy Solutions
    • TS Grewal Class 11 Accountancy Solutions
  • CBSE Sample Papers
  • NCERT Exemplar Problems
  • English Grammar
    • Wordfeud Cheat
  • MCQ Questions

CBSE Notes for Class 7 Computer in Action – Looping Statements in QBASIC

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.

  1. The PRINT statement is used to display numbers, messages or values of variables on the output screen.
  2. The REM statement is used to write a remark or a comment in the program that does not get
  3. The CLS statement is used to clear the output screen.
  4. The LET statement is used to assign a value to a variable.
  5. The INPUT statement is used to take a value from the user and store it in a variable.
  6. 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.

  1. FOR….. NEXT
  2. 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,

  1. 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.
  2. 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

cbse-notes-for-class-7-computer-in-action-looping-statements-in-qbasic-1

cbse-notes-for-class-7-computer-in-action-looping-statements-in-qbasic-2

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.statements

cbse-notes-for-class-7-computer-in-action-looping-statements-in-qbasic-4
The output of this program is:

cbse-notes-for-class-7-computer-in-action-looping-statements-in-qbasic-5

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:

cbse-notes-for-class-7-computer-in-action-looping-statements-in-qbasic-6

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:

cbse-notes-for-class-7-computer-in-action-looping-statements-in-qbasic-7

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:

cbse-notes-for-class-7-computer-in-action-looping-statements-in-qbasic-8
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:

cbse-notes-for-class-7-computer-in-action-looping-statements-in-qbasic-9

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:

cbse-notes-for-class-7-computer-in-action-looping-statements-in-qbasic-10

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:

cbse-notes-for-class-7-computer-in-action-looping-statements-in-qbasic-11

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.

  1. DO WHILE…LOOP
  2. 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

cbse-notes-for-class-7-computer-in-action-looping-statements-in-qbasic-12

cbse-notes-for-class-7-computer-in-action-looping-statements-in-qbasic-13

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:

cbse-notes-for-class-7-computer-in-action-looping-statements-in-qbasic-14

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 :
cbse-notes-for-class-7-computer-in-action-looping-statements-in-qbasic-15

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:

cbse-notes-for-class-7-computer-in-action-looping-statements-in-qbasic-16

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.

cbse-notes-for-class-7-computer-in-action-looping-statements-in-qbasic-17

Let us write the same program using the two types of DO…LOOP.

cbse-notes-for-class-7-computer-in-action-looping-statements-in-qbasic-23
The two programs give the same output:

cbse-notes-for-class-7-computer-in-action-looping-statements-in-qbasic-18

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.

cbse-notes-for-class-7-computer-in-action-looping-statements-in-qbasic-19

cbse-notes-for-class-7-computer-in-action-looping-statements-in-qbasic-20

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"

cbse-notes-for-class-7-computer-in-action-looping-statements-in-qbasic-21

Words to Know

  1. Loop: It is used to repeat the execution of a group of statements many times.
  2. Looping statements: Statements that enable you to create loops.

 

Primary Sidebar

NCERT Exemplar problems With Solutions CBSE Previous Year Questions with Solutoins CBSE Sample Papers
  • The Summer Of The Beautiful White Horse Answers
  • Job Application Letter class 12 Samples
  • Science Lab Manual Class 9
  • Letter to The Editor Class 12 Samples
  • Unseen Passage For Class 6 Answers
  • NCERT Solutions for Class 12 Hindi Core
  • Invitation and Replies Class 12 Examples
  • Advertisement Writing Class 11 Examples
  • Lab Manual Class 10 Science

Recent Posts

  • Understanding Diversity Question Answer Class 6 Social Science Civics Chapter 1 NCERT Solutions
  • Our Changing Earth Question Answer Class 7 Social Science Geography Chapter 3 NCERT Solutions
  • Inside Our Earth Question Answer Class 7 Social Science Geography Chapter 2 NCERT Solutions
  • Rulers and Buildings Question Answer Class 7 Social Science History Chapter 5 NCERT Solutions
  • On Equality Question Answer Class 7 Social Science Civics Chapter 1 NCERT Solutions
  • Role of the Government in Health Question Answer Class 7 Social Science Civics Chapter 2 NCERT Solutions
  • Vital Villages, Thriving Towns Question Answer Class 6 Social Science History Chapter 9 NCERT Solutions
  • New Empires and Kingdoms Question Answer Class 6 Social Science History Chapter 11 NCERT Solutions
  • The Delhi Sultans Question Answer Class 7 Social Science History Chapter 3 NCERT Solutions
  • The Mughal Empire Question Answer Class 7 Social Science History Chapter 4 NCERT Solutions
  • India: Climate Vegetation and Wildlife Question Answer Class 6 Social Science Geography Chapter 8 NCERT Solutions
  • Traders, Kings and Pilgrims Question Answer Class 6 Social Science History Chapter 10 NCERT Solutions
  • Environment Question Answer Class 7 Social Science Geography Chapter 1 NCERT Solutions
  • Understanding Advertising Question Answer Class 7 Social Science Civics Chapter 7 NCERT Solutions
  • The Making of Regional Cultures Question Answer Class 7 Social Science History Chapter 9 NCERT Solutions

Footer

Maths NCERT Solutions

NCERT Solutions for Class 12 Maths
NCERT Solutions for Class 11 Maths
NCERT Solutions for Class 10 Maths
NCERT Solutions for Class 9 Maths
NCERT Solutions for Class 8 Maths
NCERT Solutions for Class 7 Maths
NCERT Solutions for Class 6 Maths

SCIENCE NCERT SOLUTIONS

NCERT Solutions for Class 12 Physics
NCERT Solutions for Class 12 Chemistry
NCERT Solutions for Class 11 Physics
NCERT Solutions for Class 11 Chemistry
NCERT Solutions for Class 10 Science
NCERT Solutions for Class 9 Science
NCERT Solutions for Class 7 Science
MCQ Questions NCERT Solutions
CBSE Sample Papers
NCERT Exemplar Solutions LCM and GCF Calculator
TS Grewal Accountancy Class 12 Solutions
TS Grewal Accountancy Class 11 Solutions