Você está na página 1de 6

Loops And Repetition

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.

In BASIC the following methods are provided to support repetition:

FOR ... NEXT


DO ... LOOP UNTIL
WHILE ... WEND

The FOR loop


Basic form is:

FOR counter = start_value TO end_value STEP step_value


: : statements : :
NEXT counter

The statements between the FOR...NEXT are repeated.


A counter is used to control the number of times the loop is performed. Whwn the
loop starts the counter is set to start_value and incremented by the step_value each time
the loop is repeated. The repetition stops when the counter reaches the end_value.
(Note the step_value can be omitted when its increment is 1)

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

This loop will print the numbers from 1 to 5.


It starts by setting the variable counter to 1
Counter Printed
Loop starts 1 1
Loop repeats 2 2
Loop repeats 3 3
Loop repeats 4 4
Loop repeats 5 5
Counter reaches 5 loop stops

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

Initial value of index is 1.


Index is incremented by 1 Print index * 2
on each loop.
Loop ends when value of
index is 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 : : :

reply = inputbox("Another go? [Y/N]")


LOOP UNTIL Reply = "N"

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

The loop is repeated until


counter reaches the value 10.
Until counter = 10

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.

What does the following loop do?


index = 1
WHILE index < 12
picOutput.PRINT index * 2
index = index + 1
WEND

As a flowchart

Index = 1

While index < 12


Loop is repeated while
index is less than 12.

Index =index + 1 Continue program

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

3) Write a program to calculate the sum of the numbers from 1 to 100.

Use a loop to count from 1 to 100


Within the loop accumulate the sum of the numbers.
The answer should be 5050

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.

4) Write a program to print all of the multiplication tables from 1 to 10.

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

Year Principal Interest 10% Balance


1 100.00 10.00 110.00
2 110.00 11.00 121.00
3 121.00 12.10 133.10
4 133.10 13.31 145.41
5 145.41 14.54 159.95

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

Você também pode gostar