loops brent m. dingle texas a&m university chapter 7 – part b (and some from mastering turbo...

14
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)

Upload: theodore-craig

Post on 17-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 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)

Loops

Brent M. DingleTexas A&M University

Chapter 7 – part B(and some from Mastering Turbo Pascal 5.5, 3rd

Edition by Tom Swan)

Page 2: 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.

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.

Page 3: 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 (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.

Page 4: 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 ExamplePROGRAM UntilCount;VAR count : integer;BEGIN count := 10; REPEAT writeln(count); count := count – 1; UNTIL (count = 0);END.

Page 5: 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 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?

Page 6: 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 Second ExamplePROGRAM UntilEx2;VAR count, c2 : integer;BEGIN count := 435; REPEAT writeln(‘Howdy’); count = count + 1; UNTIL (count > 10);END.

Page 7: 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 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?

Page 8: 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)

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.

Page 9: 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)

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.

Page 10: 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)

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

Page 11: 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 Third ExamplePROGRAM RepeatAlpha;VAR ch : char;BEGIN ch := ‘Z’; REPEAT write(ch); ch := pred(ch); UNTIL ( ch < ‘A’ ); writeln;END.

Page 12: 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 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.

Page 13: 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)

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 ! =)

Page 14: 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)

End part B