pascal programming iteration (looping) carl smith national certificate unit 4

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

Upload: basil-stevens

Post on 17-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

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

Pascal Programming

Iteration (looping)

Carl SmithNational Certificate Unit 4

Page 2: Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4

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 4place plate on tableplace knife on right side of plateplace fork on left side of plate

Page 3: Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4

The FOR statement in Pascal

The FOR statement is used for definite looping such as:-

FOR control variable := start value TO end value DO

BEGINstatements to be repeated(known as the loop body)

END;

Page 4: Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 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.)

Page 5: Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4

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.

Page 6: Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4

FOR Statement 3… Note: The start value and end value

can be specified as a literal value, a variable name or an expressione.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.

Page 7: Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4

Sample code

FOR counter := 1 TO max DOBEGINwriteln (counter);writeln (counter * counter)END;

Note: max will need defining in your list or variables

Page 8: Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4

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.

Page 9: Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4

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' DOwrite(letter);

Page 10: Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4

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.

Page 11: Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4

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’

Page 12: Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4

While Example

‘While’ is often used in File Handling while not Eof(InFile) do

begin

ReadLn(InFile, Line);

WriteLn(OutFile, Line);

Inc(LineCount);

end;

Page 13: Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4

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; ... statement until expression

Page 14: Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4

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');

Page 15: Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4

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

Page 16: Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4

Loops with Arrays Loops allow programs

to address individual elements in sequence, using the index (i)

E.g. For i:= 1 to 7 DO

1

2

3

4

5

6

7

name

Page 17: Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4

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

problem Using Loops with Arrays