LOOPS
LOOPS
Computer Programming
Loops
Chapter - 8
Loop Structure
L
Loops are the important parts of most programs today. They allow repetitive tasks to be
programmed easily. They also help to reuse old lines of code we have already run.
O
What is a loop?
A loop is used to repeat a block of statements a certain number of times until a condition is met. Each
O
pass through the loop is called an iteration.
There are three ways to execute loops in QBASIC.
H
The FOR...NEXT statement
The DO...LOOP statement
The WHILE...WEND statement
SC
The most popular are the FOR...NEXT and DO...LOOP statements.
The FOR...NEXT and WHILE...WEND statements are entry controlled loop structures i.e. only
if the condition is satisfied the control passes through the body of the loop whereas the DO...LOOP
O
structure is an exit controlled structure i.e. Even if the condition is false the loop gets executed
once and gets terminated.
N
incremented steadily.
In games where you have to monitor continuously for keyboard input or mouse input.
C
It is an entry controlled loop structure. The loop terminates when the condition is false.
The syntax is as follows:
New
Regular Series Book 6
87
TechnoSchool Computer Programming
In the FOR...NEXT statement a block of instructions is executed for certain number of times. The value
of the variable counter is incremented by 1 until it equals to the end value. The variable counter, by
default, increases by 1. You can change the rate at which the variable counter increments after each
iteration by using the STEP parameter:
L
O
Test FALSE
Condition
O
TRUE
H
Body of the loop
Statements following
the loop
SC
Example 1:
This program prints Integers ranging from 1 to 10
O
DIM counter AS INTEGER
FOR counter = 1 to 10
PRINT counter
N
NEXT
H
In Example1 the instruction PRINT counter is executed 10 times. After each loop the value of the
variable counter is incremented by 1.
C
Example 2:
This program prints odd numbers ranging from 1 to 10
TE
Note: The FOR...NEXT loop is the most flexible loop structure. Use the CONTROL-BREAK
key sequence to break an infinite loop.
New
Regular Series Book 6
88
TechnoSchool Computer Programming
L
Counter variable incrementation
WEND
O
In the WHILE...WEND statement, a block of instructions is executed for certain number of times.
O
The value of the variable counter is incremented by 1 manually until it equals to the end value.
The block diagram of WHILE...WEND loop structure is as follows:
H
Block diagram of a WHILE...WEND loop structure
SC
Test FALSE
Condition
O
TRUE
Body of the loop
Statements following
N
the loop
Increment Counter
H
C
Example :
To print integers from 1 to 5
TE
DIM x AS INTEGER
x=0
WHILE x < 5
x=x+1
PRINT x
WEND
New
Regular Series Book 6
89
TechnoSchool Computer Programming
L
(OR)
DO
O
[List of instructions]
LOOP WHILE (condition)
O
While a counter is automatically incremented in the FOR...NEXT statement, you will have to
increment the counter yourself with the DO...LOOP statement.
H
The block diagram of DO...LOOP structure is as follows:
Block diagram of a DO...UNTIL/WHILE loop structure
SC
Body of the loop
O
Test FALSE
Condition
N
H
Below is an example, which makes a comparison of the FOR...NEXT and DO...LOOP methods:
The two codes below are equivalent.
TE
Using FOR...NEXT
New
Regular Series Book 6
90
TechnoSchool Computer Programming
Using DO...LOOP
L
PRINT “Hello! How are you”
counter = counter + 1
O
LOOP UNTIL counter = 5
O
Library Functions
Introduction to Library Functions
H
Library Functions are predefined functions, which has a collection of executable code that can be
reused in a program. With the help of these library functions certain tasks can be done easily.
SC
Library functions are otherwise known as Built-in functions. Library functions can be classified
into:
String Functions
Numeric Functions
O
String Functions
The functions that manipulate string values are string functions. The string functions are:
N
LEN() MID$()
H
LEFT$() LCASE$()
RIGHT$() UCASE$()
C
A$ = “Hello”
PRINT LEN (A$)
The above statement will print number 5 since the word “Hello” has 5 characters.
LEN (A$) - This function is used to find the length of the string A$.
PRINT LEN (A$) prints the value of the string A$ (i.e) the length of the string A$.
New
Regular Series Book 6
91
TechnoSchool Computer Programming
L
O
RIGHT$ (name, # of characters)
This function is used to tell the computer to start reading/printing from the right side of the string to
O
the left side. The number of characters is given after the name of the variable.
RIGHT$(A$, 4) - Return 4 characters starting from the right of the string A$.
H
PRINT RIGHT$(A$, 4) - prints 4 characters starting from the right side of the string A$.
SC
O
N
the right side. The number of characters is given after the name of the variable.
LEFT$ (A$, 3) - Returns 3 characters starting from the left side of the string A$.
C
PRINT LEFT$ (A$, 3) - prints 3 characters starting from the left side of the string A$.
TE
New
Regular Series Book 6
92
TechnoSchool Computer Programming
L
Example:
O
O
H
LCASE$(string)
SC
Converts any uppercase letters in a string to lowercase.
LCASE$ ( “ HELLO ” )
Example:
O
N
H
UCASE$(string)
C
Example:
New
Regular Series Book 6
93
TechnoSchool Computer Programming
Numeric Functions
The functions that manipulate numbers are numeric functions. The numeric functions are:
L
ABS (X) - Absolute value of X.
Example:
O
O
H
SC
SGN (number)
Returns +1 if number is positive, 0 if number is 0, and -1 if number is negative
Example:
O
N
H
C
SQR (X)
Returns square root of a positive number (Only if X > = 0 )
TE
Example:
New
Regular Series Book 6
94
TechnoSchool Computer Programming
INT (number)
It rounds a numeric value down to the next whole number that is less than or equal to the given number.
Example:
L
O
O
H
RND(number)
For any given number, RND returns a number between 0 and 1.
SC
To generate random number between a range you must describe the lowest and highest numbers
desired.
Example 1 :
O
N
H
C
Example 2 :
TE
New
Regular Series Book 6
95
TechnoSchool Computer Programming
Quick Review
A loop is used to repeat a block of statements a certain number of times until a condition is
met.
Each pass through the loop is called an iteration.
L
There are 3 ways to execute loops in QBASIC.
The FOR...NEXT statement
O
The DO...LOOP statement
The WHILE...WEND statement
O
The most popular looping statements are the FOR...NEXT and DO...LOOP statements.
The FOR...NEXT and WHILE...WEND statements are entry controlled loop structures.
H
DO...LOOP statement is an exit controlled loop structure.
The FOR...NEXT statement is used to repeat a block of instructions a specified number of
times.
SC
The WHILE...WEND loop terminates when the condition is not satisfied.
The block of instructions gets executed at least once even if the condition is false in a
DO...LOOP.
O
Library Functions are predefined functions, which has a collection of executable codes that
can be reused in a program.
N
String Functions
Numeric Functions
C
Formative
Assessment
I. Fill in the blanks.
1. Each pass through the loop is called _______________
2. _______________ and _______________ are entry controlled loop structures.
3. By default, the FOR loop variable is increased by _______________
New
Regular Series Book 6
96
TechnoSchool Computer Programming
L
O
II. Choose the correct answer.
1. __________________ structure is an exit controlled loop structure.
a. While Wend b. Do...Loop c. For Next
O
2. The parameter used to increase the variable value in For loop is __________________ .
a. Next b. FOR c. STEP
H
3. Library functions are otherwise known as __________________ .
a. String function b. Numeric function c. Built-in function
SC
4. __________________ converts any uppercase letter in a string to lowercase.
a. LCASE$ b. UCASE$ c. LUCASE$
5. __________________ returns the square root of a positive number.
a. SQR(X) b. ABS c. INT(X)
O
III. Try in your Computer and state the output of the following.
Segment 1: Segment 2:
N
NEXT PRINT x
WEND
TE
Segment 3: Segment 4:
counter = 6 For i = 10 TO 1 step -2
DO PRINT i
PRINT “Hello” NEXT
counter = counter + 1
LOOP UNTIL counter> = 10
New
Regular Series Book 6
97
TechnoSchool Computer Programming
Segment 5: Segment 6:
DIM counter AS INTEGER CLS
FOR counter = 1 TO 10 STEP 3 DIM counter AS INTEGER
PRINT counter counter = 0
NEXT DO
L
PRINT “Hello! How are you”
counter = counter + 1
O
LOOP UNTIL counter = 5
O
IV. Explain the following functions.
H
2. Right$() function 5. SGN() function 8. ABS() function
SC
3. Mid$() function 6. SQR() function 9. RND() function
O
Summative
Assessment
N
2. WHILE WEND.
3. DO UNTIL.
C
4. DO WHILE.
L
5. Mention the different String functions.
6. Mention the different Numeric functions.
O
VIII. Brain Storming.
1. Why do you need loops in programming?
O
...............................................................................................................................................
..............................................................................................................................................
H
2. What is the major difference between entry controlled loop and exit controlled loop.
...............................................................................................................................................
SC
..............................................................................................................................................
TEST FLIGHT
O
2. Write a simple program to display your School name and display the number of characters in it.
3. Write a simple program to display “CONTROL STATEMENT” for 100 times using DO loop.
H
Creative Moments
Write a program to display the sum of the first 10 natural numbers.
C
Glossary
TE