CS2 5.4.1 Nested Loop
CS2 5.4.1 Nested Loop
"Complexity has and will maintain a strong fascination for many people. It
is true that we live in a complex world and strive to solve inherently
complex problems, which often do require complex mechanisms.
However, this should not diminish our desire for elegant solutions, which
convince by their clarity and effectiveness. Simple, elegant solutions are
more effective, but they are harder to find than complex ones, and they
require more time which we too often believe to be unaffordable require more time,
which we too often believe to be unaffordable "
-Niklaus Wirth
LG-UBALDO-CS2-5.4.1.1 CS 2 Page 1 of 7
IGNITE Time Allotment: 15 minutes
From the previous learning modules we have learned about the loop control structure. We have
learned that it is a structure that repeats statement(s) while a given condition is true. Loops can
be in a form of counter controlled or event-controlled. Loops can either be a pre-test loop or a post-
test loop which is commonly used in forms of for and while loop (pre-test) and do-while loop(post-
test).
Similar to nested IF, loops can also contain another loop inside it. This structure is similarly
called, nested loops. Nested loops are sometimes called a “loop inside loop”. In a specific program,
the number of loop depends on problem complexity. The more the program is complex, the more the
loop. Nested loops can either be in a form of:
LG-UBALDO-CS2-5.4.1.1 CS 2 Page 2 of 7
Basically, a nested while loop must at least have one while loop statement inside a while loop
statement. A while loop may also contain another loop inside its loop.
A nested while loop will have a general form and syntax as shown below:
initialization;
while(condition)
{
while(condition)
{
statement(s);
updateExpression;
}
statement(s); // you can put more statements.
updateExpression;
}
In a nested loop, the outer loop’s condition is initially checked, if it results to true the program will
proceed to the inner loop, otherwise it skips the inner loop and its other statements inside. When the
inner loop is done executing, the program will check again the outer loop’s condition and will again
proceed to the inner loop if it is still true. We can see here that the inner loop is repeatedly executed
based on a condition.
Example 1:
#include <iostream>
using namespace std
int main(
int i=0
while(i<=2)
cout<<"outer loop executes"<< endl
int j=0
while(j<=3)
cout<<"inner loop executes "
cout << "i = "<<i<<" and j="<<j << endl
j++
i++
return 0
Output:
LG-UBALDO-CS2-5.4.1.1 CS 2 Page 3 of 7
{
If we examine the inner loop of the example above, we can quickly evaluate that the inner loop
executes 4 times, from 0 to 4 of variable j. But because of the outer loop, which we can evaluate to
execute 3 times, from 0 to 2 of variable i, we can further evaluate that the inner loop executes a total
of 12 times. That is 3 (outer loop execution) times 4 (inner loop execution) equals 12.
Example 2:
#include <iostream>
using namespace std
int main(
int x=1,y
while (x <= 5
y=1
while (y <= x
cout <<y
y++
return 0
Output:
1
1 2
1 2 3
1 2 3 4
In the program, the nested loop prints a pattern of numbers. The outermost loop executes 5 times in
every loop, while the inner loop runs depending on the value of x(which is 1 initially).
As we all know, the FOR loop works quite similarly like the WHILE loop. It is the concise or
compressed version of the WHILE loop. A nested FOR loop is a FOR loop inside a FOR loop.
LG-UBALDO-CS2-5.4.1.1 CS 2 Page 4 of 7
1
LG-UBALDO-CS2-5.4.1.1 CS 2 Page 5 of 7
Example 1:
#include <iostream
using namespace std
int main (
int j = 0
for(int j = 0; j < 5; j++
cout << "i = " << i << " and j = " << j << endl;
}
return 0
Output:
i = 0 and j =
i = 0 and j =
i = 0 and j =
i = 0 and j =
i = 0 and j =
i = 1 and j =
i = 1 and j =
i = 1 and j =
i = 1 and j =
i = 1 and j =
i = 2 and j =
i = 2 and j =
i = 2 and j =
i = 2 and j =
i = 2 and j =
LG-UBALDO-CS2-5.4.1.1 CS 2 Page 6 of 7
>
Nested loops are loop statements that are inside another loop statement. The most
commonly used nested loops are nested FOR loop, nested WHILE loop and nested DO-WHILE
loop. In each type of loop, you can combine any type of loop you wish to use. In C++, nesting
statements can be done until 256 level.
REFERENCES
Nested Loops in C++ with Examples. (2019, December 03). Retrieved from GeeksforGeeks: https://
www.geeksforgeeks.org/nested-loops-in-c-with-examples-2/
Parewa Labs Pvt. Ltd. (n.d.). C++ Nested Loop. Retrieved from Programiz: https://
www.programiz.com/cpp-programming/nested-loops
Shrestha, S. (2021). Nested Loops in C++ Programming. Retrieved from Programtopia: https://
www.programtopia.net/cplusplus/docs/nested-loops
Prepared by: ROMAR JAN M. UBALDO Reviewed by: Trextan Thaddeus Sanchez
Position: SSTIII Position: SSTIII
Campus: PSHS-IRC Campus: PSHS-SMC
© 2020 Philippine Science High School System. All rights reserved. This document may contain proprietary informa on and may only be
released to third par es with the approval of management. Document is uncontrolled unless otherwise marked; uncontrolled
documents are not subject to update no ca on.
LG-UBALDO-CS2-5.4.1.1 CS 2 Page 7 of 7
ti
ti
fi
ti
ti