0% found this document useful (0 votes)
4 views7 pages

Loops in Qbasic

The document provides an overview of loops in QBASIC, explaining their purpose and structure, including the FOR-NEXT and WHILE-WEND loops. It details the components of a loop, such as initialization, condition checking, and updating the loop control variable, along with examples of various programs. Additionally, it introduces accumulators and their role in storing values during iterations.

Uploaded by

Rummi Khatun
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Loops in Qbasic

The document provides an overview of loops in QBASIC, explaining their purpose and structure, including the FOR-NEXT and WHILE-WEND loops. It details the components of a loop, such as initialization, condition checking, and updating the loop control variable, along with examples of various programs. Additionally, it introduces accumulators and their role in storing values during iterations.

Uploaded by

Rummi Khatun
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

LOOPS IN QBASIC

A loop is a particular structure in programming language that allows us to execute a line


or a few lines of code repetitively without writing those lines again and again. This
process of repeating statements is also known as iteration. It makes an action easier to
do multiple times.
Thus, basic objective of a loop is:
 to execute some lines of code repeatedly.
 to keep a track of the number of times those lines are executed.
 to be able to stop the execution when required.
Since we are talking about recording the number of times these lines will be executed we
need to have a variable that will count the number of occurences. This variable that
acts as a counter is known as a loop control variable (LCV).
PARTS of A LOOP
A loop has the following parts:
1. Initialisation of the loop control variable. (LCV)
2. Conditional expression that checks whether the LCV satisfies the required condition
or not.
3. Updation of the LCV which ensures that the counter is either incremented or
decremented.
4. Body of the loop which contains all the line(s) of the code that should be repeated.
TYPES OF LOOPS IN QBASIC
1. FOR-NEXT loop
2. WHILE- WEND loop
ACCUMULATOR
Accumulators are variables having a special purpose. They accumulate and store the
values of certain operations within a loop. They rewrite new values after considering the
already existing value. Thus the accumulator stores the final value after execution of the
loop is completed. Generally, an accumulator is initialized to 0 if a sum has to be
accumulated or to 1 if a product has to be accumulated.
General form of an accumulator is:
SUM=SUM+LCV
PROD=PROD * LCV
STRUCTURE OF FOR-NEXT LOOP
Syntax:
FOR LCV=initial value TO final value [STEP updation value]
<Statements to be repeated>
NEXT LCV
(Note: By default the STEP value is +[Link] STEP keyword is omitted then the LCV has a
default value of +1)
EXAMPLES:
Program 1:WAP to display all Output:
numbers from 1 to 5 1
CLS 2
FOR A=1 to 5 3
PRINT A 4
NEXT A 5
END
Program 2: WAP to display “*” 5 Output:
times *
CLS *
FOR A=1 to 5 *
PRINT “*” *
NEXT A *
END
Program 3: WAP to print all whole Output:
numbers till 20 in the same line 0 1 2 3…10….20
CLS
FOR A=0 to 20
PRINT A;
NEXT A
END
Note* - put a ; or , after the print
statement to display in same line.
Program 4:WAP to print all Output:
natural numbers from 1 to 10 1
CLS 2
FOR A=1 to 10 .
PRINT A .
NEXT A 10
END
Program 5: WAP to print first N Output:
natural numbers 1
CLS .
Input “Enter value of N..”, N .
FOR A=1 to N .
PRINT A .
NEXT A .
END N
Use of STEP keyword in FOR-NEXT loop.
The STEP keyword contains a value, either positive or negative. It is the value which is
added to or subtracted from the LCV. The STEP keyword is optional. If it is omitted then
the LCV has a default value of +1.
EXAMPLE:
Program 1: WAP to display the CLS
following: FOR A=0 to 20 Step 2
0 PRINT A
2 NEXT A
4 END
.
.
20

Program 2: WAP to display the CLS


following: FOR A=20 to 40 Step 5
20 PRINT A
25 NEXT A
30 END
35
40

Use of Reverse Step


EXAMPLE:
Program 1: WAP to display the CLS
following: FOR A=50 to 10 Step -10
50 PRINT A
40 NEXT A
30 END
20
10

List of programs on FOR-NEXT loop

1. 1. WAP to display odd numbers till 25 3. 2. WAP to display N natural numbers


using step. and their sum.
2. CLS 4.
FOR I=1 to 25 STEP 2 5. CLS
Print I SUM=0
Next I INPUT “Enter range”, N
END FOR I=1 to N
SUM=SUM+I
PRINT I
Next I
PRINT “The sum is”, SUM
END
[Link] to accept a number display factorial of a 4. WAP to accept 10 numbers from
number. The factorial of a number (N!) is equal to the keyboard and find their sum and
1*2*3…..Upto N. average.
CLS
INPUT “Enter a number to display its factorial”, N CLS
FACT =1 SUM=0
FOR I=1 to N FOR I=1 to 10
FACT=FACT * I INPUT “Enter number”, N
Next I SUM=SUM+N
PRINT “The factorial is”, FACT Next I
END PRINT “The sum is”, SUM
PRINT “The average is”, SUM/10
END

6. 5. WAP to generate the multiplication table for 7. [Link] to display even numbers from
an entered number up to 15. 30 to 10 in reverse order.

CLS CLS
INPUT “Enter a number to display its multiplication FOR I=30 to 10 STEP -2
table:” N Print I
FOR I=1 to 15 Next I
PRINT N; “*“; I; “= “; N * I END
NEXT I
END

8. 7. WAP to display the terms and sum of the 9. 8. WAP to display the product of the
following series: following series:
1 + 4 +7 +10 + 13 +16+19+22 1.0 * 1.25 * 1.50 * 1.75 * 2.0

CLS CLS
SUM=0 PRODUCT = 1
FOR I=1 to 22 STEP 3 FOR I=1 to 2 STEP 0.25
Print I PRODUCT = PRODUCT*I
SUM=SUM+I Next I
Next I PRINT “The product of the series is “,
PRINT “The sum of series is “, SUM PRODUCT
END END

9. WAP to display sum of the following series: 11.


10. 1+1/2+1/3+1/4……..N terms
CLS
INPUT “Enter range”, N
SUM=0
FOR I=1 to N
SUM=SUM+1/I
Next I
PRINT “The sum of series is “, SUM
END
WHILE-WEND LOOP

This loop allows the body of the loop to be executed as long as the given condition is true. This is
used when the number of iterations is not known.

STRUCTURE OF WHILE-WEND LOOP


Syntax:
Initialisation of LCV
WHILE condition
<body of the loop>
Updation of the LCV
WEND
EXAMPLE:

WAP to print first 10 natural numbers Output


CLS 1
LET I=1 2
WHILE I<=10 3
PRINT I 4
I=I+1 5
WEND 6
END 7
8
9
10

WAP to display all numbers from 50 to 100 Output


using while wend loop 50
CLS 51
I=50 52
.
WHILE I<=100 .
PRINT I .
I=I+1 .
WEND .
END 100

Concept of digit extraction using WHILE-WEND loop

WAP to input a number and display the Eg


digits of the number Input 356
CLS
INPUT “Enter a number”, N Output
PRINT “The digits of the entered number are..”
WHILE N>0 The digits of the entered number are..
DIGIT=N MOD 10
PRINT DIGIT; 653
N=N\10
WEND
END
Program list on WHILE-WEND loop

1. Write a program to display the following 2. Write a program to find sum of the
series using while wend loop: numbers from 1 to 10 using while
1 8 27…….N terms wend loop:

CLS
CLS
S=0
INPUT “Enter range”, N
I=1
I=1
WHILE I<=10
WHILE I<=N
S=S+I
PRINT I^3
I=I+1
I=I+1
WEND
WEND
Print “The sum is”, S
END
END

[Link] a program to find product of the 4. Write a program to display the sum of
numbers from 1 to N terms using the following series using while
while wend loop: wend loop:
S=0 + 3 + 8 + 15…….N terms
CLS
CLS
INPUT “Enter range”, N INPUT “Enter range”, N
P=1 I=1
I=1
WHILE I<=N
WHILE I<=N S=S+(I^2 - 1)
P=P*I
I=I+1
I=I+1 WEND
WEND
Print “The sum is”, S
Print “The product is”, P
END
END
[Link] a program to display the following 6. Write a program to accept a number and
series using while wend loop: display the factors of the
50 40 30 20 10 number(including the number itself) using
while wend loop.
CLS CLS
INPUT “Enter a number”, N
I=50
WHILE I>=10 I=1
PRINT I; WHILE I<=N
IF N MOD I=0 then
I=I-10
PRINT I
WEND
END IF
END
I=I+1
WEND
END
7. Write a program to accept a number and 8. Write a program to accept a number and
find the sum of the digits using find the product of the digits
while wend loop. using while wend loop.
CLS CLS
INPUT “Enter a number”, N INPUT “Enter a number”, N
S=0 P=1
WHILE N>0 WHILE N>0
DIGIT= N MOD 10 DIGIT= N MOD 10
S=S+DIGIT P=P*DIGIT
N=N\10 N=N\10
WEND WEND
Print “The sum of digits is”, S PRINT “The product of digits is”, P
END END

[Link] a program to accept a number and [Link] a program to accept a number and
display the reverse of the check if the number is an
number using while wend loop. Armstrong number or not. Display
appropriate message.
CLS
INPUT “Enter a number”, N CLS
REV=0 INPUT “Enter a number” , N
WHILE N>0 B=N
DIGIT= N MOD 10 WHILE N>0
REV= REV *10+DIGIT DIGIT= N MOD 10
N=N\10 S= S+(DIGIT * DIGIT * DIGIT)
WEND N=N\10
Print “The reverse number is”, REV WEND
END IF S=B then
PRINT “ It is an Armstrong number”
ELSE
PRINT “ It is not an Armstrong number”
END IF
END

You might also like