140 likes | 151 Views
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. REPEAT [statement] UNTIL [expr] Notice, [expr] works differently here than in a WHILE-DO loop.
E N D
Loops Brent M. DingleTexas A&M University Chapter 7 – part B (and some from Mastering Turbo Pascal 5.5, 3rd Edition by Tom Swan)
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.
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.
REPEAT-UNTIL Example PROGRAM UntilCount; VAR count : integer; BEGIN count := 10; REPEAT writeln(count); count := count – 1; UNTIL (count = 0); END.
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?
REPEAT-UNTIL Second Example PROGRAM UntilEx2; VAR count, c2 : integer; BEGIN count := 435; REPEAT writeln(‘Howdy’); count = count + 1; UNTIL (count > 10); END.
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?
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.
succ • The function succ(x) returns the value that comes AFTER whatever value x currently has. • For example:succ( 1 ) returns 2succ( 28 ) returns 29succ( ‘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.
pred • The function pred(x) returns the value that comes BEFORE whatever value x currently has. • For example:pred( 1 ) returns 0pred( 28 ) returns 27pred( ‘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 RepeatAlpha; VAR ch : char; BEGIN ch := ‘Z’; REPEAT write(ch); ch := pred(ch); UNTIL ( ch < ‘A’ ); writeln; END.
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.
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 ! =)