Loops Brent M Dingle Texas AM University Chapter
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)
Repeat Until Format n n n 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.
Repeat – Until (cont) n n n 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.
REPEAT-UNTIL Example PROGRAM Until. Count; VAR count : integer; BEGIN count : = 10; REPEAT writeln(count); count : = count – 1; UNTIL (count = 0); END.
REPEAT-UNTIL Example n n 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?
REPEAT-UNTIL Second Example PROGRAM Until. Ex 2; VAR count, c 2 : integer; BEGIN count : = 435; REPEAT writeln(‘Howdy’); count = count + 1; UNTIL (count > 10); END.
REPEAT-UNTIL Second Example n n 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?
New Functions n succ = successor (that which comes after) n pred = predecessor (that which comes before) n These functions are discussed in 8. 3, but are useful for doing cool things with loops.
succ n n n 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 order – like integers, or letters in the alphabet. Real numbers are not ordinal, after 1. 0 comes what? 1. 1? 1. 001? 1. 0001? … -> no answer.
pred n n n 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().
REPEAT-UNTIL Third Example PROGRAM Repeat. Alpha; VAR ch : char; BEGIN ch : = ‘Z’; REPEAT write(ch); ch : = pred(ch); UNTIL ( ch < ‘A’ ); writeln; END.
REPEAT-UNTIL Third Example n n n 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.
ASCII character codes n n n Find out what ASCII character codes are. What does the function ord() do? How about chr()? Go find out ! Impress your friends ! =)
End part B
- Slides: 14