Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loops Brent M. Dingle Texas A&M University Chapter 7 – part D (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 D (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 D (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)

2 Using loops for input Loops are often used to do input – for example a while NOT (eof (infile) ). Or while NOT (all stuff input from user). With this idea comes the idea that input is just a list of data (a list of names, a list of numbers, a list of some combination of both, etc). If the input is a list, how might a loop be used? How would it know when it was at the end of the list?

3 Methods to terminate an INPUT loop. List headed by size The list begins with a number indicating how long the list is. Ask before iterating (repeating) After every input the user is asked if they want to continue inputting stuff. List ended with a sentinel value. Some impossible value is put at the end of the list. For example if the list is of colors use BOB as a sentinel value, as BOB is not a valid color, the program would know there was no more input. Running out of input. There simply is no more input – eof is an easy example of this method.

4 Non-Input Loops Not all loops are input loops. Since we have terminating methods for input-loops, we must have methods for terminating non-input loops. These lists of methods tend to make good multiple choice questions.

5 Methods to terminate NON-input loops Loop is count controlled. There is an initial value and a final value. Ask before iterating The user is asked if they want the program to loop during the course of each loop. Exit on sentinel value Somehow an impossible value was encountered (usually calculated at runtime, but defined somewhere else). Running out of data (“input”) There is no more data to perform the operations of the loop on. Exit on a flag condition. Some variable (a flag) is automatically set within the loop that indicates the status of whether the loop should continue. When the variable’s value says ‘stop’ then the loop ends.

6 Nested Loops Nested loops are loops containing loops. Often these are used to create tables (as tables have rows and columns).

7 Nested Loops – Example PROGRAM MultTab; VAR row, col, prod : integer; BEGIN writeln(‘Multiplication Table’); writeln (‘ * 0 1 2 3 4 5’); FOR row := 0 to 10 DO Begin write(row:4); FOR col := 0 to 5 DO begin prod := row * col; write(prod:4); end; { for col } writeln; End; { for row } END.

8 Nested Loops (cont) Notice the output of the above program has 11 rows and only 6 columns (recall we started with zero). Alter the program so it has 10 rows AND 10 columns. I would STRONGLY encourage you to figure out how nested loops work.

9 Definitions Assertions Comments such as preconditions (what was true before a loop) and postconditions (what is true after a loop) are called assertions. We are asserting that such things are true. Invariant A loop invariant or invariant is an assertion that is true before a loop and true after a loop. Variant expression An expression for a quantity (or a variable) that changes each time a loop is executed is called a variant expression.

10 More Problems (not graded) page 265  13, 14 page 271 – 273  16, 17, 18, 24, 26 and maybe 21

11 End part D And so ends Chapter 7


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

Similar presentations


Ads by Google