Control Structure and Repetition: By: Group 4
Control Structure and Repetition: By: Group 4
Repetition
By: Group 4
Repetitive control structures, also referred to as iterative
structures, are groupings of code which are designed to
repeat a set of related statements. This repetition (or
iteration) can repeat zero or more times, until some
control value or condition causes the repetition to
cease. We use iterative controls when we need to do
the same task more than once, based upon some
logical condition. While the terms repetition and iteration
are very descriptive words, the common term to
describe these control structures is loop. Loops consist of
two logical parts; the condition (i.e. the logic that
evaluates the condition), and the loop body (i.e. where
the code integral to the loop is located). I categorize
loops into two general categories:
Indeterminate loops
The difference being that with a determinate loop
structure, one can (normally) predict exactly how many
times the loop will repeat; whereas with an
indeterminate loop structure, this is not always the case.
An indeterminate loop structure loops as long as a
condition evaluates to some certain boolean value.
Indeterminate loops should be used when the
programmer does not know exactly how many times the
iteration need occur. Logically however, both
indeterminant and determinate loops can be written to
be equivalent.
Beginning with indeterminate looping
structures, there are two types to introduce:
Pre-test loops
Post-test loops
A pre-test loop evaluates its condition before
any statements contained in its body are
executed. Thus, a pre-test loop can execute
its body a minimum of zero times, assuming
the initial value of the condition evaluates to
False. A post-test loop on the other hand,
evaluates its condition after it executes all
statements in its body. Thus, a post-test loop
can execute its body a minimum of
one time, even if the initial value of the
condition evaluates to False. Below are
diagrams representing of each type of loop:
When considering pre-test loops, the standard
example is the Do-While loop. The general syntax
of the Visual Basic Do-While can be viewed a
couple of ways:
Do While conditions Do While conditions
{ statement (s) } { statement (s) }
Loop statement affecting condition
Loop
The structure on the left above is technically
correct syntax, but perhaps not logically so.
Technically, a loop body can contain zero
statements; but if that is the case, the loop will
never terminate. A loop that never terminates is
called an infinite loop, and typically is not a
desirable event. The structure above on the right
mandates at least one statement that will affect
the condition, thus hopefully causing the loop to
end. Once the loop ends, program execution
will continue at the first statement following the
end of the loop.
The condition in any loop must evaluate to a
Boolean value; as always, either True or False. The
condition in a loop will be similar to the logic and
syntax used in an If-Then statement. That is, the
condition will consist of relational and/or logical
operators. It is the condition that is perhaps the most
difficult part of a loop; if the logical condition is
incorrect, there is little hope that the loop will
produce the desired results. It is also of import that
the condition which will cause the loop to terminate
must occur. Looking at a Do-While loop structure
whose task is to sum the numbers between 1 and 5,
we can observe the following (note the numbers to
the left are for descriptive purposes only):
In computer science,
the Boolean data type is a data type
that has one of two possible values
(usually denoted true and false),
intended to represent the two truth
values of logic and Boolean algebra.
It is named after George Boole, who
first defined an algebraic system of
logic in the mid 19th century.
1.Dim intCount As Integer = 1
2.Dim intSum As Integer
3.Do While intCount <= 5
4.intSum = intSum + intCount
5.intCount = intCount + 1
6.Loop
Looking closer at each numbered
statement above:
statement 1 declares an Integer variable, intCount, and initializes it to 1. This
will be used as our counter or control variable, which will allow termination
of the loop.
statement 2 declares another Integer variable to be used to store our
summary total (i.e. sum). It is not intialized, but recall any variable not
specifically initialized is set to zero by VB.
statement 3 is the start of our Do-While loop construct. Note that our logical
condition here is intCount <= 5. It should be understood that the condition
intCount <= 5 must be True for the loop body to continue to execute.
statement 4 accumulates the value of intCount into intSum.
statement 5 increments the counter; it is this statement that will eventually
cause our condition to be False.
finally, statement 6 is the formal end to the loop.
Below is a table of values for each variable
as well as the corresponding Boolean
condition as the loop iterates:
intCount intSum intCount <= 5 Comment
Do Do
[ statement (s) ] [ statement (s) ]
Loop Until condition statement affecting condition
Loop until condition
As above, the structure on the left above is technically correct
syntax, but perhaps not logically so. Technically, a loop body can
contain zero statements; but if that is the case, the loop will never
terminate. Notice here the condition is checked at the bottom of
the loop, thus the loop body will always be executed at least once.
Looking at a similar Do-Until loop to sum the numbers between 1
and 5, we observe:
For intCount = 1 To 5
intSum = intSum + intCount
Next
Which achieves and equivalent result. One important difference to
note from the other two loop structures is that in a For-Next loop, the
increment of the control variable happens automatically. That is,
incrementing the counter variable is built in to the loop behavior
(note this is step # 5 in the two loop examples above). This is
commonly a confusing point for students.
As in the examples above, the value of intSum upon termination of
this loop would be 15. However, it is important to look at the value
intCount would have upon loop termination. As this loop will only
terminate when intCount > 5, the value of that variable would then
be 6. Any usage of this variable will result in using the value 6. This is
a very important side effect of all loops in general.
Note you could also write the above For loop as:
For intCount = 5 To 1 Step -1
and achieve an equivalent result, however this may be logically
more difficult to understand.
When looking at the following two For statements,
we might ask the simple question "how many times
will each loop, loop?"
For intCount = -5 To 5
For intCount = 0 To 15 Step 2
While we might be able to calculate the result using our fingers, a more
accurate approach is by using the following formula to calculate exactly how
many iterations a loop will perform:
# iterations = Int2((end_value - start_value) / step_value) + 1
Which when step_value = 1 simplifies to
# iterations = end_value - start_value + 1
Thus, in the example # 1 above, the number of iterations would be:
(5 - -5) + 1 or 11
and example # 2 would result in
Int ((15 - 0) / 2) + 1, or 8
Make sure you clearly understand how each of these results were found.
Note this step implies if start _value > end_value and step_value> 0, the For-Next
loop will iterate zero times. The same is true for the logical inverse of this.
Control structures
IF a = 0 THEN
ROOT ← −c/b
ELSE
DISCRIMINANT ← b*b − 4*a*c
IF DISCRIMINANT ≥ 0 THEN
ROOT ← (−b + SQUARE_ROOT(DISCRIMINANT))/2*a
ENDIF
ENDIF
The SQUARE_ROOT function used in the above fragment
is an example of a subprogram (also called a procedure,
subroutine, or function). A subprogram is like a sauce
recipe given once and used as part of many other
recipes. Subprograms take inputs (the quantity needed)
and produce results (the sauce). Commonly used
subprograms are generally in a collection or library
provided with a language.
Subprograms may call other subprograms in their definitions, as
shown by the following routine (where ABS is the absolute-value
function). SQUARE_ROOT is implemented by using a WHILE
(indefinite) loop that produces a good approximation for the
square root of real numbers unless x is very small or very large. A
subprogram is written by declaring its name, the type of input data,
and the output:
FUNCTION SQUARE_ROOT(REAL x) RETURNS REAL
ROOT ← 1.0
WHILE ABS(ROOT*ROOT − x) ≥ 0.000001
AND WHILE ROOT ← (x/ROOT + ROOT)/2
RETURN ROOT
Subprograms can break a problem into smaller, more tractable
subproblems. Sometimes a problem may be solved by reducing it
to a subproblem that is a smaller version of the original. In that case
the routine is known as a recursive subprogram because it solves
the problem by repeatedly calling itself. For example, the factorial
function in mathematics (n! = n∙(n−1)⋯3∙2∙1—i.e., the product of
the first n integers), can be programmed as a recursive routine:
IF n = 0 THEN RETURN 1