Concepts of Programming Languages

(Sean Pound) #1

368 Chapter 8 Statement-Level Control Structures


else
loopBody()
forLoop loopBody, (reps - 1);;

In this function, the parameter loopBody is the function with the body of the
loop and the parameter reps is the number of repetitions. The reserved word
rec appears before the name of the function to indicate that it is recursive. The
empty parentheses do nothing; they are there because in F# an empty statement
is illegal and every if must have an else clause.

8.3.2 Logically Controlled Loops


In many cases, collections of statements must be repeatedly executed, but the
repetition control is based on a Boolean expression rather than a counter. For
these situations, a logically controlled loop is convenient. Actually, logically
controlled loops are more general than counter-controlled loops. Every count-
ing loop can be built with a logical loop, but the reverse is not true. Also, recall
that only selection and logical loops are essential to express the control struc-
ture of any flowchart.

8.3.2.1 Design Issues
Because they are much simpler than counter-controlled loops, logically con-
trolled loops have fewer design issues.


  • Should the control be pretest or posttest?

  • Should the logically controlled loop be a special form of a counting loop
    or a separate statement?


8.3.2.2 Examples
The C-based programming languages include both pretest and posttest logi-
cally controlled loops that are not special forms of their counter-controlled
iterative statements. The pretest and posttest logical loops have the following
forms:

while (control_expression)
loop body

and

do
loop body
while (control_expression);
Free download pdf