W7 Module Through The Loops
W7 Module Through The Loops
“Any idea, plan, or purpose may be placed in the mind through repetition of
thought.”
–Napoleon Hill.
Now that you are enabled to work with conditionals which diverts selected
statements depending on the conditions given, we need to enable you to have
your application repeat a series of statements depending on another
condition. This way, we do not need to repeat writing exactly the same
statements whenever we need some statements to be repeated.
So what are we waiting for? Let us continue our exploration of the world of
Computer Programming.
Course Module
Recall
In the previous module, we discussed about code blocks and how variables
are affected by code blocks. In addition, we covered the two conditionals in
C++ and differentiated them, namely:
1. IF Statements
a. if(…) { }
b. if(…) { } else { }
c. if(…) { } else if(…) { } else { }
2. SWITCH Statements
a. switch(…) { case …: break; default: …; }
3. Nested Conditionals
Lastly, we explored a sample problem wherein we dissected several
possibilities when using conditionals in C++.
Introduction to Looping
Body is the phase in looping statements where the statements inside the loop
are executed. These are simply the statements within the code block of the
looping statement. The statements within the body differ depending on the
discretion of the developer.
C++ is armed with three (3) distinct looping statements which differ mainly
on how the condition is evaluated, and when the condition is evaluated. The
looping statements in C++ are:
a) While Loop
b) For Loop
c) Do-While Loop
While Loop
Course Module
Figure 1. WHILE Statement
Figure 1 shows the syntax of the WHILE statement. Notice that the
initialization phase occurred at line 8, outside of the actual looping
statement. The condition phase is placed within the parentheses of the
WHILE statement at line 10. The body is from line 11 to 12. Lastly, notice that
the increment/decrement phase is located within the body at line 12.
If we follow the program flow, the WHILE statement will display “Hello
World” prepended with the value of the counter i + 1. If we try to understand
the condition within the WHILE statement, the iterations will end when the
counter i reaches the value of 9. Hence, the “Hello World” line will be
displayed 9 times.
If you did not get why there will be 9 lines of “Hello World” displayed, try
counting up to before 9 (which is 8). Remember to count from 0.
For Loop
For Loop is the looping statement in C++ where the initialization, condition
and increment/decrement phases occur within the parentheses of the
statement. Only the body is separated from the actual FOR statement.
Compared to WHILE statements, FOR statements is range-based. This means
that the counter is essentially the blood and life of this looping statement.
Let us look at the syntax and structure of a FOR statement in Figure 2. This
example uses the same logic and flow like our WHILE statement in Figure 1.
The only difference is that we used FOR rather than WHILE statement.
Course Module
2. In line 9, the body of the FOR statement where the string “Hello
World” is displayed.
While the WHILE statement seems a straight forward approach. FOR
statement is otherwise. The flow when the initialization, the condition and
the increment/decrement phase occur is unclear if you simply look at the
structure.
The first thing the FOR statement does is it executes the initialization phase.
This occurs only once, when the FOR statement is called. Next, the condition
is evaluated. When the condition returns false, the body is executed. After
which, the increment/decrement is executed. Here one iteration is
completed. The condition is again evaluated and when it returned false, the
body is again executed. The cycle repeats until the condition evaluates to
true.
As you have now understood, the cycle is almost similar to WHILE statement.
The only main difference is when the increment/decrement is executed. In
the WHILE statement, the increment/decrement can occur before the other
statements of the body; while in FOR statement, the increment/decrement
always occur after the statements of the body.
Though a rare occurrence, you can use more than one counter in FOR
statement. The counters are separated by a comma. The same applies to the
increment/decrement. Try experimenting!
Do While Statements
The last looping statement in C++ is the DO WHILE statement. The DO WHILE
statement is closely similar to the WHILE statement. They only differ when
the condition is evaluated and when the body is executed. Let us look at an
implementation of the DO WHILE statement.
Figure 3. DO-WHILE Statement
What does this suggest? In the above example, we cannot easily distinguish
the effects of this kind of approach. However, if we change the value of our
counter i to something greater or equal to the constant 9, there we will notice
the big difference. Both in WHILE and FOR, the body will not be executed
since the condition is evaluated first which evaluated to true. But in DO
WHILE, the body will be executed regardless if the condition evaluates to
true later on. By the time the condition is evaluated, the counter i has already
been incremented/decremented.
Controlling Loops
Since the start of the module, when dealing with looping statements,
everything has been linear and straight forward. However, this is not always
the case when we implement our solutions. In C++, we have two controlling
statements which can change the flow of loops at any part of the iteration.
These are the BREAK and CONTINUE statements.
The BREAK statements terminate the execution of the entire loop that it is
encased. This is placed anywhere within the body of the loop. Any statements
found after the BREAK statement, should the BREAK statement be executed,
will not be executed.
Course Module
The CONTINUE statements terminates the execution of the current iteration.
What it does is force the loop to skip the rest of the statements and proceed
to the condition evaluation right away.
Glossary
Break Statement: A statement that terminates the execution of the entire
loop.
Body [Phase]: A phase in looping where the statements within the code
block are executed.
Condition [Phase]: A phase in looping where an expression or series of
expression are evaluated which determines whether the loop should
continue or end.
Continue Statement: A statement that terminates the execution of the
current iteration.
Counter (Looping): A dedicated variable used in looping which manages the
flow of the current iteration.
Do While Statement: A looping statement where the initialization also occur
outside of the actual looping statement, but the body is executed first before
the condition is evaluated.
For Statement: A range-based looping statement.
Increment/Decrement [Phase]: A phase in looping where the counter
undergo a linear change in value by a constant.
Initialization [Phase]: A phase in looping where the counter variable is
assigned its initial value.
Iteration: One pass in a loop.
Looping: A means in programming languages to repeat a series of
statements directed by the set conditions.
While Statement: A looping statement where the initialization occur outside
of the actual looping statement, and the condition is evaluated first before the
body is executed.
References and Supplementary Materials
Books and Journals
Stephen Davis; 2014; Beginning Programming with C++ For Dummies, 2 nd Edition; United
States; For Dummies, A Wiley Brand
Richard Halterman; 2017; Fundamentals of C++ Programming; United States of America;
Southern Adventist University
Course Module