Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loops Brent M. Dingle Texas A&M University Chapter 7 – part B (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)

Similar presentations


Presentation on theme: "Loops Brent M. Dingle Texas A&M University Chapter 7 – part B (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)"— Presentation transcript:

1 Loops Brent M. Dingle Texas A&M University Chapter 7 – part B (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)

2 Repeat Until Format REPEAT [statement] UNTIL [expr] Notice, [expr] works differently here than in a WHILE-DO loop. While the boolean expression [expr] is FALSE the computer will repeatedly perform [statement]. So be sure that [expr] will eventually become TRUE – else you will be in an infinite loop. Note: the exit condition of the repeat-until loop is the condition that makes [expr] TRUE.

3 Repeat – Until (cont) Every REPEAT-UNTIL loop executes at least once. The value of [expr] is not checked until the statement has been executed. Notice also a REPEAT-UNTIL does NOT use a BEGIN-END pair for a compound statement.

4 REPEAT-UNTIL Example PROGRAM UntilCount; VAR count : integer; BEGIN count := 10; REPEAT writeln(count); count := count – 1; UNTIL (count = 0); END.

5 REPEAT-UNTIL Example The previous example will output the numbers 10 down to 1 on the screen. Something to think about is what value does count have after the loop ends?

6 REPEAT-UNTIL Second Example PROGRAM UntilEx2; VAR count, c2 : integer; BEGIN count := 435; REPEAT writeln(‘Howdy’); count = count + 1; UNTIL (count > 10); END.

7 REPEAT-UNTIL Second Example How many times will the above loop execute? How many times is Howdy written to the screen? If you initialized count := 1, what would the answers to the above questions be? If you initialized count := 5, what would the answers be?

8 New Functions succ = successor (that which comes after) pred = predecessor (that which comes before) These functions are discussed in 8.3, but are useful for doing cool things with loops.

9 succ The function succ(x) returns the value that comes AFTER whatever value x currently has. For example: succ( 1 ) returns 2 succ( 28 ) returns 29 succ( ‘A’ ) returns ‘B’ succ( ‘d’ ) returns ‘e’ You may only send ordinal data types to succ(). Ordinal data types are things that have a definite finite order – like integers, or letters in the alphabet. Real numbers are not ordinal, after 1.0 comes what? 1.1? 1.01? 1.001? 1.0001? … -> no answer.

10 pred The function pred(x) returns the value that comes BEFORE whatever value x currently has. For example: pred( 1 ) returns 0 pred( 28 ) returns 27 pred( ‘A’ ) returns hmmm… you go check that out. pred( ‘d’ ) returns ‘c’ You may only send ordinal data types to pred().

11 REPEAT-UNTIL Third Example PROGRAM RepeatAlpha; VAR ch : char; BEGIN ch := ‘Z’; REPEAT write(ch); ch := pred(ch); UNTIL ( ch < ‘A’ ); writeln; END.

12 REPEAT-UNTIL Third Example The above example will output the alphabet (uppercase) backwards on the screen. Try altering the program so it will output it in lowercase. Try altering it (use an if statement?) to make it output uppercase z to a and then lowercase Z to A.

13 ASCII character codes Find out what ASCII character codes are. What does the function ord() do? How about chr()? Go find out ! Impress your friends ! =)

14 End part B


Download ppt "Loops Brent M. Dingle Texas A&M University Chapter 7 – part B (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)"

Similar presentations


Ads by Google