Program Correctness 57
computes the value of the expression on the right-hand side of the equal sign and then stores
the result in the location indicated by the name on the left-hand side. To cause branching
in the code, we use a condition test of the form
if condition then
S1
else
S 2
When this code is executed, the condition is evaluated to be either TRUE or FALSE. If the
condition is TRUE, then the code represented by S1 is executed, the code represented by
S2 is not executed, and the execution then continues at the first command following S 2 .If
the condition is FALSE, then the code represented by S2 is executed, the code represented
by S1 is not executed, and the execution then continues at the first command following S 2.
For a statement that can cause repetition of a block of code, we generally use a for
construct. The code
for i = 1 to n do
S
starts by initializing i to the value 1. If the value of i is less than or equal to n, the commands
represented by S will then be executed. The code S may or may not use i as a variable. At
the end of executing this indented code, i will be incremented by 1 and then tested to see
if it is still less than or equal to n. If this condition for the current value of i is evaluated
as TRUE, the loop is executed again using the new value of i. When the condition is tested
with a value of i for which the condition is evaluated as FALSE, the program continues at
the next line following the code represented by S. We often refer to such code as a for loop.
To display a result, we use the word print followed by a list of the names of the storage
locations whose values are to be displayed (think of print on the screen or of output to a
printer). Comments in the code will appear as /* any text as a comment */. Comments are
skipped over when the program is executed.
With just these four instructions (assignment, condition, repetition, and printing) for
pseudocode, we can write instructions that could easily be turned into valid code in some
programming language.
An additional way to cause repetition of a block of code is with use of a while loop:
while condition
S
A while statement is a command to execute the code indented below the while state-
ment over and over again as long as the condition written just after the word while is
evaluated as TRUE. If this condition is evaluated as FALSE when the loop is first reached,
the indented statements are executed zero times, that is, they are not executed.
Many authors use the word algorithm to describe only strategies for programs that
will ultimately stop. Others would say there is no "output" unless it stops. We include our
apologies for our use of the word and present the following program as algorithm. The
algorithm will use a while loop to "repeat forever" a block of code, since the condition
in the while statement can never be false. This is just an instruction to execute the while
loop without stopping-or until someone turns off the computer. In this case, it is called
an infinite loop, since it could go on forever!