CLASS NOTES:
SD110 Computer Programming Logic
 
 

 

LOOPS
 
 
OVERVIEW:
 
   


Loops are areas of code which repeat over and over a certain number of times, or until certain conditions have been met. The type of loop you choose to use will be determined in part by what you are trying to accomplish, and part by what seems more logical to you. Get to know the various loop methods and how to implement them.

WHILE, DO WHILE AND DO UNTIL LOOPS

These loops continue to execute until a certain condition occurs to Break them out of the loop. Some Statement within each of these loops much change a Variable to eventually meet the Break condition. While the Flowchat can be identical for these loops, pick the Loop that works for you.

Once again you are out to buy toys, and this time you have a lot of money with you.
How many toys can you get?

In this example you will continue to buy toys until you have less than $10 and can no longer afford them. The While loop and the Do While loop accomplish the same tasks with a minor difference in syntax. The Do Until loop also breaks from the loop when the condition is met, but with two notable differences: First, that the loop continues while the Expression remains False (the opposite of the other two which continue while the condition remains True) and that the Do-Until always processes at least once. In the above example what happens if you arrive with less than $10 to begin with? The Do-Until loop would let you buy a toy, change your cash to a negative number, and then Break from the loop. This is an error, so be careful when using Do-Until loops.

FOR NEXT LOOPS

Another Loop is the FOR-NEXT loop. This type of loop is sometimes referred to as a Definite Loop because you know how many times the loop will repeat. You know this because you set the number within the syntax of the loop itself.

FOR VariableName = 1 TO 100
  DoSomething
NEXT VariableName

This Loop initially sets the VariableName to 1, then it performs whatever actions are in the DoSomething section, then it increments VariableName by 1 and returns to the top to run again. This Loop will run exactly 100 times before it is done. The default increment is 1, but that can be altered by adding a Step Command to the Loop which lets you manually determine how the VariableName will be incremented, both in how much and in what direction.

Q: How many times will each of the FOR-NEXT loops below run?

A:

FOR VariableName = 0 TO 100 Step 2
  DoSomething
NEXT VariableName

B:

FOR VariableName = 100 TO 0 Step -10
  DoSomething
NEXT VariableName

C:

FOR VariableName = 20 TO 200 Step 20
  DoSomething
NEXT VariableName

D:

FOR VariableName = 1 TO 11 Step 3
  DoSomething
NEXT VariableName

 

 

 

ANSWERS:

A: 51 - Runs with the value 0 and all even values up to and including 100
B: 11 - Runs with the values 100, 90, 80, 70, 60, 50, 40, 30, 20, 10 and 0
C: 10 - Runs with the values 20, 40, 60, 80, 100, 120, 140, 160, 180 and 200
D: 4 - Runs with the values 1, 4, 7 and 10

   
     
 
BACK