1 / 14

Loops

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.

sgoforth
Download Presentation

Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Loops Brent M. DingleTexas A&M University Chapter 7 – part B (and some from Mastering Turbo Pascal 5.5, 3rd 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 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.

  10. 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().

  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

More Related