Loops
Loops
The programs written so far have performed a task once only and then stopped. For a
computer program to be useful some of the program statements will need to be repeated.
For example, it is unlikely only one payslip is to be printed, it is more likely that
several (even hundreds) of payslips will be printed at any one time. A program to print
payslips must therefore repeat the process as many times as there are payslips to print.
Example
FOR counter = 1 TO 5 [step value is 1
picOutput. PRINT counter [value of counter is printed
NEXT [loop repeats until counter is 5
Loops 1
What does the following loop do?
FOR index = 1 TO 12
picOutput.PRINT index * 2
NEXT index
As a flowchart
For index = 1 to 12
Next index
Program continues
The DO loop
The FOR statement is useful when you know how many times you want to perform the
loop. There are times however when you will not know how many times you want to
perform the loop.
The DO loop has a comparison (or test) at the end of the loop. The comparison is used
to determine whether the loop should be repeated or not.
Example.
DO
: : : programming statements : : :
The statements inside the DO....LOOP will be repeated UNTIL the user enters "N" when
asked if they wish to have another go.
Loops 2
This loop is used when you know that the statements inside the loop must be
performed at least once.
DO
: : : statements : : :
LOOP UNTIL logical comparison
Example
Predict what will appear on screen when the following loop is run:
counter = 0
DO
counter = counter + 1
picOutput.PRINT counter
LOOP UNTIL counter = 10
As a flowchart
Counter = 0
Repeat
Counter = counter + 1
Print Counter
Continue program
Loops 3
The WHILE loop
You may wish to perform the loop only while a certain condition is true.
WHILE logical_comparison
: : : statements : : :
WEND
Example
number=0
WHILE number < > 999
number = Inputbox("Enter number to square")
IF number < > 999 THEN picOutput.Print number * number
WEND
This loop accepts a number from the user and, if the number is not 999 displays the
square of that number, ie if 4 is entered then 16 is displayed. The loop continues until
the number 999 is entered.
Note that the test takes place at the start of the loop.
As a flowchart
Index = 1
Print index
Loops 4
Loop Exercises Using FOR..NEXT, WHILE..WEND or DO..LOOP
1) Write a program to print the 5 times table in a picture box as shown below.
picOut
1x5=5
2 x 5 = 10
3 x 5 = 15
4 x 5 = 20
5 x 5 = 25
6 x 5 = 30
7 x 5 = 35
8 x 5 = 40
9 x 5 = 45 Start End
10 x 5 = 50
2) Adapt the program above so that the user can choose which table to print
1 x 10 = 10
2 x 10 = 20
picOut 3 x 10 = 30 10
4 x 10 = 40 Select Table to Print
5 x 10 = 50 txtValue
6 x 10 = 60
7 x 10 = 70
8 x 10 = 80
9 x 10 = 90 Start End
10 x 10 = 100
4) Given the following code attached to the Start button. State what would appear in the
picture box when the button is pressed?
Counter = 1
Do
For X = 1 to 10
Value = Value * X
PicOut.Print Value
A=0
While A < 2
PicOut.Print “ “; ‘a space
Start A=A+1
Endwhile
If Value < 10 then
picOut.Print “ “; ‘a space
EndIf
PicOut Next X
PicOut.Print
Counter = Counter + 1
Loop Until Counter = 10
Loops 5
More Loop Exercises Using FOR..NEXT, WHILE..WEND or
DO..LOOP
1) Write a program that asks the user to enter their name and counts the number of
vowels and consonents in their name.
2) Write a password program that allows the user 3 attempts to log on.
3) Write a program to input 20 numbers and calculates their total and average.
Solve the problem using a For loop, a While loop, and a Do loop.
5) Write a program to calculate the compound interest on a given sum each year over
a given period and print a table similar to the one below:
Principal : 100
Interest Rate : 10
Number of Years : 5
6) Write a program to accept a list of numbers and print out the smallest number in
the list.
7) Write a program that accepts three numbers and prints them out in ascending
order.
8) Write a program that calculates the lowest common multiple for two values.
Example, the lowest common multiple of 21 and 35 is 7 because it is the highest
value that divides exactly into each number.
Loops 6