T contains several statements that control the sequence of program execution. Each of these control statements must be used entirely within the program module or a subprogram module.
An exit statement has the form:
exit [when boolean expression]and is allowed only within a loop statement or a for statement. The exit statement causes program execution to jump to the first statement after the nearest enclosing loop or for statement. If the optional key word when is present, the exit is conditional and occurs when the boolean expression is true.
A continue statement has the form:
continue [when boolean expression]and, as above, is allowed only within a loop statement or a for statement. The continue statement causes program execution to jump to the first statement in the nearest enclosing loop or for statement. If the optional key word when is present, the jump is conditional and occurs when the boolean expression is true.
The loop control statement has the form:
loop
declarations and statements
end loop
Program execution jumps to the first statement in the loop body on reaching end
loop. Note that, by itself, a loop statement is infinite; that is, it will continue
indefinitely unless stopped by some other statement. An exit statement terminates
the nearest enclosing loop. Declarations made within a loop are visible only within
the loop body.
As an exercise, enter and run the following program. Try using the debug mode of the interpreter to observe the behavior of the exit statement.
var number : int := 0
program
loop
put number
watch(number)
exit when number > 3
number := succ(number)
end loop
end program
On your own, try adding a continue statement to the program above and observe its
behavior.
The for control statement is written as:
for [decreasing] name := begin...end do
declarations and statements
end for
The range following the ":=" defines the beginning and ending values of the count
variable name. The count limits begin and end must be integer expressions. The
loop's statement list is executed once for each valid value of the count variable
which is incremented or, if decreasing is included, decremented by one before
repeating the statement list. As above, an exit statement can be used to terminate
the loop. Declarations made within the for loop are not visible outside of the loop.
Try this program:
var number : int := 0
program
var i : int
for i := 1...5 do
number := pred(number)
watch(number)
continue when number = -3
put number
end for
end program
On your own, try adding an exit statement to the program.
An if control statement has the form:
if boolean expression then
declarations and statements
{elsif boolean expression then
declarations and statements}
[else
declarations and statements]
end if
The boolean expression for each branch is evaluated until one of them is
true. The statements in the branch are executed until a closing elsif,
else, or end if is reached. If no boolean expression is true then the
statements following else, if present, are executed. The program resumes
at the first statement after end if.
Try this:
program
put "Enter grades one at a time"
loop
var mark : int
put ": "...
get mark
exit when mark < 0
if mark > 100 then
put "Invalid"
elsif mark >= 93 then
put "A"
elsif mark >= 85 then
put "B"
elsif mark >= 78 then
put "C"
elsif mark >= 70 then
put "D"
else
put "F"
end if
end loop
end program
Try adding an unconditional exit to the loop in the program above.
A case control statement has the form:
case expression of
value constant{, constant} :
declarations and statements
{value constant{, constant} :
declarations and statements}
[value :
declarations and statements]
end case
The expression and each constant must be of matching type of either
integer, character, string, or an enumerated type. Declarations made
within a branch are not visible outside the branch.
The expression is evaluated and compared with each constant of each branch until one of them is true. The statements in the branch are executed until another value or end case is reached. If no match is found then the statements following an optional value without a constant are executed. The program resumes at the first statement after end case.
Try this example:
program
var word : string
put "enter a word from:"
put "the rain in spain"
loop
put ": "...
get word
watch(word)
case word of
value "the", "rain", "in":
put "ok"
value "spain":
put "done"
exit
value:
put "not ok"
end case
end loop
end program
Try using the if statement to write a program which behaves just like
the program above.
The goto statement causes an unconditional jump from one point in a list of statements to a named location. Jumps must be entirely within a module. In order to use a goto statement, a name of the location to jump to must be declared using the form:
The goto statement can then be coded as:label name : % no type!
This statement can be used to simplify your code by enabling jumps out of deeply nested logic or by jumping to a single point of return in a subprogram. It can also be used to make your program difficult to understand. On your own, try writing a program which uses the goto statement.goto name % from here . . . name : % to here
This chapter showed you how to use T's control statements. Each is a statement by itself and may be contained within other control statements. You should be able to design almost all conceivable decision logic using these statements.