You are on page 1of 10

MODULE #4

BATAAN HEROES COLLEGE


Balanga City, Bataan

Computer Fundamentals
and Programming

Prepared by:

ANGELA G. CRUZ
Business & Technology Department
Bataan Heroes College

This module or any portion thereof may not be reproduced or used in any manner whatsoever without the
express written permission of the publisher except for educational purposes but with a citation to this source.

For Permission : Contact Bataan Heroes College, Roman Super Hi-way, Balanga City, Bataan, Philippines

Computer Fundamentals and Programming 2


ANGELA G. CRUZ
Bataan Heroes College

STUDY GUIDE NO. 4

Module 4: Loop Statements


LEARNING OBJECTIVES
After studying this module, you should be able to:
1. Understand what is loop statement
2. Differentiate the loop statement in C++
3. Create a program using different loop statements

1.1 Loop Statement in C++


Loop – Allows us to execute a statement or group of statements multiple times.
Types of Loops
A. While Loop – Repeats a statement or group of statements while a given condition is true.
It test the condition before executing the body.
Syntax:
while (condition)
{
Statement
}

Computer Fundamentals and Programming 3


ANGELA G. CRUZ
Bataan Heroes College

Example: Output

Explanation:
int a; - We declared a variable named a with the data type int, meaning it can handle only
whole numbers or integer. Note that if we declared a variable and we didn’t assigned a
value its default initial value is 0. But for example we declared like this int a=5; we
declared a as integer with 5 as its initial value.
while (a<=20)
{
Cout<<”Value of a: “ <<a<<endl;
a++;
}
We will display the string “Value of a” together with the value of a while its value is less
than or equal to 20. Meaning instead we print the text using cout in multiple times, we used
loop specifically while loop to print the string multiple times while the condition is true.
a++ - is the code that will increment the value of a by 1 as the statement loops or repeats.

Computer Fundamentals and Programming 4


ANGELA G. CRUZ
B. Do – While Loop – It is like a while statement, except that it test the condition at the end
of the loop body.
Syntax:
do
{
Statement
}
while (condition)

Example: Output

int a; - We declared a variable named a with the data type int, meaning it can handle only
whole numbers or integer.
do
{
Cout<<”Value of a: “ <<a<<endl;
a++;
}
while(a>=20)

Computer Fundamentals and Programming 5


ANGELA G. CRUZ
Bataan Heroes College

It’s the same thing with while condition, it has the same purpose which is to run a specific
statement or group of statements multiple times depending on the condition given. It’s just
that in do while we will execute first the statement (statement under do) before we check
the condition (condition inside wile).
We will also display the string “Value of a” together with the value of a while its value is
less than or equal to 20.
a++ - is the code that will increment the value of a by 1 as the statement loops or repeats.

C. For Loop – execute a sequence of statements multiple times and abbreviate the code that
manages the loop variable
Parts of For Loop
1. Init – execute first and inly once
- Allows you to declare and initialize any loop control variable
2. Condition – if it is true, the body of the loop will be executed
- If it is false, the body of the loop does not execute and flow of control jumps to the
next statement after the loop
3. Increment – After the body of the loop executes, the flow of control jumps back up to
the increment statement.
Syntax:
for (init; condition; increment)
{
Statement
}

Computer Fundamentals and Programming 6


ANGELA G. CRUZ
Example: Output:

Explanation:
int a=1; - this is our init r initialization. We decalred a vriable with 1 as its initial value
a<=20; this is the condition of our loop. Meaning the statement inside the body of loop
will execute while this condition is true.
a++ - the value of variable will increment by 1 as the statement loops or repeats.
for (int a=1; a<=20; a++)
{
cout<<"Value of a: "<<a<<endl;
}
Meaning, we will print the string “Value of a” with the value of a, and the initial value of
a is 1, so when the string execute for the first time the first value of a is 1, when the string
will execute again the value of a now is 2, it is because we have incrimination in the for
loop. The value of a will increment by 1 as the statement loop while a is less than or equal
to 20.

Computer Fundamentals and Programming 7


ANGELA G. CRUZ
Bataan Heroes College

D. Goto Loop – When encountered, program flow jumps to the location specified by goto
- The goto requires a label
- Label is a valid C++ identifier followed by colon
Syntax:
loop_name:
statement;
Example:
Output:

Explanation:
Int a= 1 – We declared a variable named a with initial value of 1.
loop:
cout<<"Value of a: "<< a <<endl;
a++;
Our loop/label name is loop, again you are the one who will name the label. Under
our loop we will display the string Value of a with the value of a. And as the loop repeats
its execution the value of a will increment by 1.
if(a<=20)
{
goto loop;
}
We used if statement to compare the value of a if it is less than or equal to 20. Since our
loop will increment by 1 as it repeats its execution, we compare the value of a to 20. If the

Computer Fundamentals and Programming 8


ANGELA G. CRUZ
value of a is less than or equal to 20, the program flow will jump to the loop. If the value of
a will become greater than 20 the program will be terminated.

E. Nested Loop - You can use one or more loop inside any another loop.
Example: Output:

Explanation:
When the first for loop is executed, the value of i is 1 and "Table of 12" gets
printed. Now coming to the second loop, the value of j is 1 and thus 1*1 = 1 gets printed.
In the second iteration of the inner for loop, while the value of i is still 1, the value
of j becomes 2 and thus 1 * 2 = 2 gets printed. In the last iteration of the inner for loop,
the value of i is still 1 and the value of j becomes 10, thus printing 1 * 10 = 10.
Now after all the iterations of the inner for loop are complete, there will be the second
iteration of the outer for loop increasing the value of i to 2 and printing Table of 2. Again
the inner for loop will be iterated with i equals 2. And so on, and so on.
We can use any loop inside any other loop according to the requirement. In the above
example, we used one for loop inside another.

Computer Fundamentals and Programming 9


ANGELA G. CRUZ
Bataan Heroes College

F. Infinite Loop - There may exist some loops which can iterate or occur infinitely. These
are called Infinite Loop. These loops occur infinitely because their condition is always
true.

References

Loops in C++. Codes Dope. Retrieved September 1, 2020 from https://www.codesdope.com/cpp-


loops/

C++ Loop Types. Tutorial Points. Retrieved September 1, 2020 from


https://www.tutorialspoint.com/cplusplus/cpp_loop_types.htm

Computer Fundamentals and Programming 10


ANGELA G. CRUZ

You might also like