Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.

Similar presentations


Presentation on theme: "Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4."— Presentation transcript:

1 Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4

2 Definite Loops Iteration is commonly referred to as looping and describes the situation where an action, or number of actions, are repeated a number of times. Sometimes it is known in advance exactly how many repetitions are needed (a DEFINITE loop) e.g. turn the wing nut anti clockwise 5 times to unlock for each person in a family of 4 place plate on table place knife on right side of plate place fork on left side of plate

3 The FOR statement in Pascal The FOR statement is used for definite looping such as:- FOR control variable := start value TO end value DO BEGIN statements to be repeated (known as the loop body) END;

4 FOR Statement 1… The control variable can be of any ordinal type i.e. Integer Char Boolean (True or False) (Note when using FOR loops this control variable needs adding to the list of variables.)

5 FOR Statement 2… When the FOR loop executes, this variable is set to the value specified in start value. After each execution of the loop body the control variable is incremented by 1 and the loop body is executed again until the control variable reaches a value that is greater than the end value specified. Execution will then continue at the statement following the END clause. Hence the control variable acts as a counter.

6 FOR Statement 3… Note: The start value and end value can be specified as a literal value, a variable name or an expression e.g. FOR counter := 3 TO (x*y) NOTE: The start value does not have to be the value 1 NOTE: If the end value is not greater than the start value the loop body will not be executed.

7 Sample code FOR counter := 1 TO max DO BEGIN writeln (counter); writeln (counter * counter) END; Note: max will need defining in your list or variables

8 Comment In the previous example the control variable is used in the body of the loop. This is a very useful programming practice, but do not attempt to change the value of this variable as this will affect the number of times the loop will execute. Notice also that there is NO semicolon on the final statement in the loop body before the END statement.

9 The DownTo clause In FOR loops, unlike many other languages the control variable can only be incremented by 1. However the value can be decremented by using the DOWNTO clause in place of TO e.g. FOR letter 'z' DOWNTO 'a' DO write(letter);

10 Indefinite Loops For many applications it is not known beforehand how many times a loop may execute. The loop will terminate when some condition has been met. This type of loop is an indefinite loop. There are two implementations of indefinite loops in Pascal: The WHILE statement The REPEAT statement. Consider the WHILE statement.

11 The WHILE statement WHILE test condition DO BEGIN statements for the body of the loop END; The WHILE loop repeats the loop body zero or more times as long as the test condition remains true. This is also known as a “preconditioned loop” as the condition is tested before the loop body is executed. This means that if the condition is false the first time, the loop body is not executed at all. The loop body can be a single statement or a statement block. The BEGIN and END statements are not needed for a single statement. NOTE if the test condition remains true then the loop will execute indefinitely and the program will ‘hang’

12 While Example ‘While’ is often used in File Handling while not Eof(InFile) do begin ReadLn(InFile, Line); WriteLn(OutFile, Line); Inc(LineCount); end;

13 REPEAT statement The statements between repeat and until are executed in sequence until, at the end of a sequence, the Boolean expression is True. Syntax: repeat statement;... statement until expression

14 Repeat Examples NOTE: The sequence is executed at least once. Example1: { Repeat Statements } repeat Ch := GetChar until Ch <> ' '; Example 2: repeat Write('Enter value: '); ReadLn(I); until (I >= 0) and (I <= '9');

15 FOR, REPEAT or WHILE? FOR should be used if the loop is to be executed a predetermined number of times As a general rule While should be used when the test is done before the loop is entered. E.g. when reading data from a file that may be empty (test for EOF) Repeat loops always execute at least once and should be used when the control check is tested on ‘exit’ i.e. repeat UNTIL something happens

16 Loops with Arrays Loops allow programs to address individual elements in sequence, using the index (i) E.g. For i:= 1 to 7 DO

17 Summary We looked at:- Definition of ‘Iteration’ For Loops While Loops Repeat Loops Choosing the correct loop for the problem Using Loops with Arrays


Download ppt "Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4."

Similar presentations


Ads by Google