Presentation is loading. Please wait.

Presentation is loading. Please wait.

Shortcoming of the FOR-DO loop When you use the FOR-DO loop you must know how many times you want to perform an action. What if you didn’t know how many.

Similar presentations


Presentation on theme: "Shortcoming of the FOR-DO loop When you use the FOR-DO loop you must know how many times you want to perform an action. What if you didn’t know how many."— Presentation transcript:

1 Shortcoming of the FOR-DO loop When you use the FOR-DO loop you must know how many times you want to perform an action. What if you didn’t know how many times an action was to be performed? For example, suppose you wanted to know how many times an action needed to be repeated before a certain condition was met.

2 The WHILE loop WHILE (some Boolean expression is true) DO some particular action i := 0;{initialization of i} WHILE i < 100 DO BEGIN Write (i, ‘ ‘); i = i + 3; End;

3 The WHILE loop (con’t) The identifiers in the Boolean expression that controls the WHILE loop must all be defined. If they are not defined by an earlier part of the program, you must initialize them (the book calls this priming) before the loop begins.

4 The WHILE loop (con’t) At least one of the statements in the loop must change the value of the Boolean expression so it will eventually become false. Otherwise, the loop will execute forever (this is called an infinite loop). In Pascal we stop an infinite loop by pressing Ctrl-Break Poor logic can also cause an infinite loop.

5 Sentinels A sentinel is a piece of data that ends a set of data but isn’t a possible value of the data For example, if we write a program that a police officer uses to enter the speed of each car (s)he gave a speeding ticket to during a week, the end of the data can be indicated by a -1.


Download ppt "Shortcoming of the FOR-DO loop When you use the FOR-DO loop you must know how many times you want to perform an action. What if you didn’t know how many."

Similar presentations


Ads by Google