Nassi-Schneiderman
Flowcharts
The
Loop Structure
The
loop structure (also called the iterative structure) is used when you
need to do something repetitively in an algorithm. Loops are characterized by two main components: a condition that must be tested each
time through, and a body that is executed, depending on whether the
condition is True or False.
The body describes some action that must be done (perhaps) many
times. The condition determines whether
you must keep doing that repetitive action.
For
example, consider English statement “I will mow my lawn while there is grass
to be cut”. This statement implies
the existence of a loop in the sense that you are (a) performing a repetitive
action (cutting rows of grass) and (b) defining a condition that tells you
whether to continue mowing (“while there is grass to be cut”).
In
general, there are two versions of the iterative structure: the pretest loop and the posttest
loop. The pretest loop is a
construct that first tests the condition of the loop, then executes the body of
the loop if the condition is met. The
posttest loop is a construct that executes the body of the loop at least once
and tests the condition at the end. In
a pretest loop, you could specify a loop that never actually gets executed, if
the condition is never satisfied. A
posttest loop always gets executed at least once.
Pretest
Loops
The
pretest loop first tests the condition of the loop and then permits a program
segment to be repeatedly executed, as long as the test condition is
satisfied. There are at least two
versions of the pretest loop: while
and for.
While
Loops
The
while (aka while-do) loop is a general loop that repeats the body
of the loop as long as its condition is true.
It tells the computer “while some condition is true, perform some
instructions repeatedly.” The diagram
below illustrates the general format of the while loop:

In
the case of the while loop (or any other loop), the statement “while some
condition is true” is replaced by some question that determines whether to keep
executing the loop. Thus, an If/Then style of decision structure is not
necessary here, because it is considered by the “while” portion of the NS
chart.
Thinking
of the general statement “while X is true, repeatedly do the following…”,
you might consider the previous real-world example: “While there is long grass, mow the lawn.” This statement can also be used to
illustrate the “execute ZERO or more times” property: if you get the mower out and examine the lawn only to find that
it has already been mowed, then the English statement is still valid (the
“While” portion turned out to be false, so you didn’t have to do any mowing).
The
diagram below illustrates a formal NS chart for that English statement:

A
more complex example is given below.
This example gives the logic for a program that will print the numbers
from one to ten.

In
the beginning of this program, a variable called count was initialized to
1. This illustrates an important
requirement with pretest loops such as the while. A pretest loop always tests a condition
before the loop body can be executed, and this condition must be a question
that can be answered. Above, if the count
variable had no initial value, then the question “while count is less than 11?”
would make no sense.
The
computer requires that this initial condition always be “set up” so that the
question testing the loop is valid.
Otherwise, the question won’t make sense to the computer. As a real-world example, when you are
eating, you might be implicitly performing a loop: “while I am hungry, eat.”
This means that there is an initial condition that exists: you are hungry. You don’t explicitly tell yourself to “be hungry” beforehand, but
that is precisely the sort of thing that must be done with a computer, as the
computer won’t know whether “it is hungry” or “count is 1” or whatever else.
For
Loops
The
for (aka for-do) loop is used when we know exactly how many
repetitions there will be. A special
variable is used for counting the number of loop iterations. The for loop tells the computer to
essentially “execute this loop precisely N times, for some positive
value of N. The diagram below illustrates the general
format of the for loop:

The
for loop is often used in mathematical situations or at any time in which you
know in advance that you are dealing with a precise number of loop
iterations. Consider a real-world
example in which you have to mow 5 yards:

In
this example, there is a while loop inside the for loop. The outer for loop determines that you will
mow exactly 5 yards. The inner while
loop determines that you will keep mowing (whatever yard) while long grass
exists.
A
final example shows an NS chart using a for loop. Here, a value K is read and the loop finds the sum of the first K
integers:
