0% found this document useful (0 votes)
19 views9 pages

W7 Module Through The Loops

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views9 pages

W7 Module Through The Loops

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Module 007 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.

At the end of this module, you will be able to:


1. Identify the components of a looping statement
2. Argue the importance of having looping statements in C++
3. Identify and differentiate the looping statements in C++
4. Identify the statements that manipulates the flow of loops

Again, researching beyond the coverage of this module is highly encouraged


to supplement your understanding of the topics covered. And as always,
think and see beyond the box.

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

Looping in C++ is a means to repeat a series of statements based on the


provided condition. One pass in the loop is called an iteration.Usually, a
looping statement undergoes several iterations.

Phases of a Looping Statement

Like any other statements of a programming language, looping is also


composed of several components. These components build up the phases
that a looping statement need to undergo internally. There are four (4)
phases of any looping statement in any programming language. These are:
a) Initialization
b) Condition
c) Body
d) Increment/Decrement

Initialization is the phase in looping statements where a variable called


counter is set an initial value. Note that declaration of the counter can occur
outside of the looping statement hence there is no need to prioritize
declaration as part of the first phase. The counter manages the flow of the
loop. Usually, each iteration relies on the value of the counter, similar as how
arrays are utilized in looping statements as will be seen in Module 008. A
typical initialization of a counter is set to 0. However, there are cases wherein
the counter is initialized with the actual number of iterations needed for the
loop. These setup is upon the discretion of the developer or as needed in the
identified solution.

Condition is the phase in looping statements where an expression is


evaluated which dictates whether the loop should stop or co ntinue. The
counter is usually the basis of this phase, however that is not always the case.
The condition always occurs within the parentheses of the loop.

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.

Lastly, the Increment/Decrement phase in the looping statements is where


the counter is incremented or decremented. Increment means a variable is
added by a certain constant. Decrement, the counterpart of increment, means
a variable is subtracted by a certain constant. This constant is typically 1.
Depending on the chosen looping statement, the Increment/Decrement can
occur within the body.

Looping Statements in C++

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

While Loop is a looping statement in C++ where the condition is evaluated


right before the body is executed. The initialization phase does not occur
within the looping statement, or the parentheses of the statement. The
increment/decrement happens at the end of the body.

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.

Let us understand the flow of the source code.


1. At line 8, a counter i is initialized with a constant zero (0).
2. At line 10, the actual WHILE statement is used. Within the statement is
the condition that tests whether the counter i is still less than the
constant nine (9).
3. At line 11, the first line of the body, a “#.) Hello World” string is displayed
with an ending line.
4. At line 12, the counter i is incremented with the constant one (1).

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.

Essentially, Figure 1 simply display the line “Hello World” 10 times.


It is essential to simulate first the source code prior execution when using
WHILE statements since it can introduce a non-terminating iterations. Simply
put, the loop will continue forever. This scenario will continually consume the
computer’s resources which can cause the operating system to malfunction and
the computer to shutdown unintendedly.

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.

Figure 2. FOR 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.

Dissecting the FOR statement in this example, we will have:


1. In line 8, the actual FOR statement is used together with the
initialization of the counter i to a constant 0, the condition which
checks the value of the counter and the increment/decrement.

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

Similar to the WHILE statement, the initialization occurred outside of the


actual looping statement. Notice that the body of the loop is located on top of
the condition. From the structure itself, we can easily notice that even during
execution, the statements within the body are executed first. After which the
condition is evaluated, checking whether to continue the loop or stop it all
together.

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

Online Supplementary Reading Materials


for loop; http://en.cppreference.com/w/cpp/language/for; June 20, 2017
Range-based for loop; http://en.cppreference.com/w/cpp/language/range-for; June 20,
2017
while loop; http://en.cppreference.com/w/cpp/language/while; June 20, 2017
do-while loop;http://en.cppreference.com/w/cpp/language/do; June 20, 2017

Course Module

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy