REPETITION LOOPS A loop is used to run a block of statements over and over. It is simply a group of commands that is repeated a specified number of times or for a specific length of time. There are two types of loops: 1. Counter loop where repetition is based on a specified number of times. 2. Conditional loop where repetition is based on set conditions.
REPETITION 1. COUNTER LOOPS You use a counter loop when you want the
computer to perform a task with a specific number of times. This is similar to running a track race of, let say 12 laps. While running, you count the number of laps, and when have completed 12, you will stop.
REPETITION A counter loop is also known as a For loop or a
For/Next loop. This is because the ends of the loop are defined by the For statement and the Next statement. A For/Next loop requires two statements; the For statement at the beginning of the loop and the Next statement at the end of the loop. At the beginning of a For loop, you define a counter variable as well as the start and end values for the variable.
REPETITION For example, if you want the loop to repeat 12 times, you would set For X = 1 To 12
The syntax of the loop is as follows For countervariable = start To end Statements to be executed Next countervariable
REPETITION Nested Loops You can nest (layered) two or more For loops inside one another. Whenever your program needs to repeat a loop more than once, use a nested loop. The following example shows two nested loops: For x = 1 To 3 For y = 1 To 5 Print “@”;
Outer loop
Next y Print Next x
Inner loop
REPETITION The inner loop is performed first. After it is completed, then the outer loop carries on.
Inner Loop In the example, the inner loop starts with the variable y=1. It prints ‘@’. The semicolon will cause the next statement to print on the same line.
Outer Loop The outer loop begins with the variable x=1. This will cause the inner loop to occur once, resulting in @@@@@. The second time the outer loop occurs, another @@@@@ will be printed, and so on.
REPETITION
2. CONDITIONAL LOOPS As an alternative to a For/Next loop, there is a conditional loop that executes a group of statements repeatedly until a certain condition is True in the loop. Conditional loops are used when you do not know in advance how many times a loop should repeat. For example, if you are writing a program to enter names into a mailing list, you may want to stop the program when the word ‘Done’ is entered. Conditional loop are often called Do loops because the word Do is the keyword that starts the set of commands in the loop. It also happens that Loop is the keyword that ends the set of commands in the loop.
REPETITION Do While/Loop The first type of conditional loop we will look at is the Do While loop. The keyword While in the statement tells the program that the loop is repeated while the condition expression is true. This is similar to waiting at a pedestrian crossing. You wait while the light is red. When the light is no longer red, you cross. Do While light is red Wait patiently Loop Cross quickly
REPETITION A Do While loop executes while the condition is
True. Then when the condition becomes False, the program moves on to the next statement after the Loop statement. The syntax is Do While condition Statement to be executed Loop nextstatement
REPETITION Do/Loop While This is similar to Do While/Loop structure. However it tests the condition after the loop body is performed. This means that the loop will be executed at least once. This form of the Do While loop places the condition in the Loop statement. Do Statement to be executed Loop While condition next statement
REPETITION Endless Loops It is important that you test your loop programs to make sure a loop does not run endlessly for a certain value of the test variable. Consider the following example: Do number = val(text1) ‘Type -1 to quit number = number*number Print number Loop While number = 0 nextstatement