Statement Level Flow of Control
Iteration Structures
Copyright © 2003-2014 by Curt Hill
Looping statements
• Iteration is fundamental• Repeating a statement or
statements is present in all languages– Even Ada Lovlace wrote a loop in
the nineteenth century– Plankakul also had a loop
• The use of loops is a large amount of the power of the computers
Copyright © 2003-2014 by Curt Hill
Looping• The early loops were often
connected to processing array data, since early programming was mostly numerical
• Just because the while is the most general looping construct does not mean that it is desirable to have only it– Writeability is improved by
several loops
Copyright © 2003-2014 by Curt Hill
Design issues• How is the iteration controlled?
– Counting, Boolean testing or combination
• Where shall the test and exit mechanism be?
• Two separate considerations– Physical placement of the
mechanism•This is generally negligible
– Logical placement before the loop body or after loop body
Copyright © 2003-2014 by Curt Hill
Location
• Tests may be:• Before
– Pretest or leading decision
• After – Posttest or trailing decision
• Anywhere in the middle
Copyright © 2003-2014 by Curt Hill
Counter controlled• This is the classic array
manipulation loop• It is less general than a boolean
controlled loop but very handy
• There is a control variable, usually integer– It is given an initial value and
tested against a terminal value– There may be a step size value to
allow counting by other than 1– These are the loop parameters
• Supported by machine instructions
Copyright © 2003-2014 by Curt Hill
Design issues
• What is the type of the control variable?
• What is the scope of the control variable?
• May the loop parameters be changed in the loop?
• How many times should the loop parameters be evaluated
Copyright © 2003-2014 by Curt Hill
The DO of FORTRAN I-IV
• Posttest• DO label variable = initial,
terminal [,step]• Support for this in the hardware• Only posttest count controlled
loop• Parameters must be unsigned
constants or positive integers, not expressions
Copyright © 2003-2014 by Curt Hill
FORTRAN 77 and 90• Loop retains same form, but
becomes leading decision• The type of the parameters is
enlarged to that of integer or real• The parameters may now be
expressions as well• They are evaluated once at the
beginning of the loop to produce the iteration count, the number of times to loop
• When loop terminates it has its most recent value
Copyright © 2003-2014 by Curt Hill
FORTRAN 90
• F90 also introduces an END DO option instead of statement label
• Control variable only allows integer
Copyright © 2003-2014 by Curt Hill
ALGOL 60• This is one of the most complicated
loops because it tried to do it all• Count controlled• List controlled• Boolean controlled
– Quit when true– Quit when false
• Stuff before the body• Stuff after the body
– See the book for some examples
• It evaluated everything every time• The control variable was declared
external to the loop
Copyright © 2003-2014 by Curt Hill
COBOL Perform varying
• COBOL has a weird and delightful loop for subscripting through arrays that may be at most 3 dimensions
• The Perform is both a pseudo function call and loop– Several forms of it
• See example on next screen
Copyright © 2003-2014 by Curt Hill
COBOL Perform
Copyright © 2003-2014 by Curt Hill
Perform ABC Varying ndx From 1 by q
Until ndx = 50 After sub from 10 by 2 Until sub > 30
After N from 12 by -1 Until sub < 2.
The Pascal for• A delightfully simple pretest count
controlled loop• Control variable is any ordinal type• Increment can only be 1 or -1• Loop variable is declared externally• Loop variable is undefined at the
end of the loop• Parameters are only evaluated
once• Assignment to the control variable
in the loop is illegal
Copyright © 2003-2014 by Curt Hill
Ada for• Somewhat similar to Pascal in
concept but different in form• for var in [reverse] range loop …
end loop• for x in 1..A loop … end loop• The control variable is declared
in the loop and assumes the same type as the range
• No assignment is allowed on the control variable
Copyright © 2003-2014 by Curt Hill
C family for• Not actually count controlled loops
since the test does not have to match the initialization or step
• Very powerful and occasionally unruly
• Book has a nice example using the comma operator
• Java the test must be boolean• C++ and Java restrict any defined
variables to the loop body
Copyright © 2003-2014 by Curt Hill
Logically controlled loops• Design issues
– Pretest or posttest?– Separate loop or special form of
counting loop?
• Examples– There are several languages that
have both a pretest and posttest loop: C, C++, Java, Pascal
– FORTRAN 77 & 90 had no pretest or posttest logical loop
– Ada has only a pretestCopyright © 2003-2014 by Curt Hill
User located test and exits• The programmer may choose
locations other than beginning and end– Modula-2 and Ada have a loop statement
which is infinite– There is a conditional exit statement
that can be put anywhere including multiple sites
– C, C++, Java have the break which can accomplish the same thing• Which is a goto to first statement following
loop
– C and C++ also have a continue which is exit the current iteration but stay in loop
Copyright © 2003-2014 by Curt Hill
Iteration based on data structures
• Several languages allow a loop similar to a counting loop but based on something other than an integer– LISP has a dolist function that executes
a piece of code for each item on the list– Perl and Lambda MOO have a similar
construct that does something for each item on the list
• This has become popular enough that Java (5) and C++ (2011) have added
Copyright © 2003-2014 by Curt Hill
Examples
Copyright © 2003-2014 by Curt Hill
Lambda MOO:for x in (list) . . . endfor
Java (5 and after) :Picture p … ;Pixel [] px = p.getPixels();for(Pixel ap: px){ // process ap }
Ada
Copyright © 2003-2014 by Curt Hill
subtype MyRange is Integer range 0.99;MyArray: array (MyRange) of Integer;for Index in MyRange loop ...MyArray(Index) ... end loop;-- Really a Boolean loop
Finally
• The next presentation will consider is the forms of the GOTO and other control flow oddities
Copyright © 2003-2014 by Curt Hill