0% found this document useful (0 votes)
2 views

CS2 5.4.1 Nested Loop

This document is a lesson plan for a module on nested loops in computational thinking, detailing the objectives, structure, and examples of nested loops in programming. It explains the concept of nested loops, including nested while and for loops, and provides code examples to illustrate their functionality. Additionally, it includes a brief assessment section to test understanding of the material covered.

Uploaded by

kch.09082014
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CS2 5.4.1 Nested Loop

This document is a lesson plan for a module on nested loops in computational thinking, detailing the objectives, structure, and examples of nested loops in programming. It explains the concept of nested loops, including nested while and for loops, and provides code examples to illustrate their functionality. Additionally, it includes a brief assessment section to test understanding of the material covered.

Uploaded by

kch.09082014
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Subject Code: CS2 Introduction to Computational Thinking

Module Code: 5.0 More Logical Control Structure


Lesson Code: 5.4.1 Nested Loop
Time Frame: 30 minutes

TARGET Time Allotment: 1 minute

After completing this module, you are expected to:


• Understand the structure of nested loops and how to use them
• Construct a nested loop program that solves a specific tasks
• Test and debug a nested loop program

HOOK Time Allotment: 4 minutes

"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).

The nested loop

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:

1. Nested while loop


2. Nested for loop
3. Nested do-while loop
Note: You can have any type of loop nested inside any type of loop. There is no restriction that you
can only use a specific type of loop to a specific type of loop.

Nested WHILE loop

A nested while loop is a while loop inside a while loop.

A nested while loop can be illustrated in a flowchart shown below.

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:

outer loop executes


inner loop executes i = 0 and j=0
inner loop executes i = 0 and j=1
inner loop executes i = 0 and j=2
inner loop executes i = 0 and j=3

LG-UBALDO-CS2-5.4.1.1 CS 2 Page 3 of 7
{

outer loop executes


inner loop executes i = 1 and j=0
inner loop executes i = 1 and j=1
inner loop executes i = 1 and j=2
inner loop executes i = 1 and j=3
outer loop executes
inner loop executes i = 2 and j=0
inner loop executes i = 2 and j=1
inner loop executes i = 2 and j=2
inner loop executes i = 2 and j=3

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++

cout << endl


x++

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).

Nested FOR loop

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.

The general form and syntax of a nested FOR loop is:


for ( initialization; condition; updateExpression ) {

LG-UBALDO-CS2-5.4.1.1 CS 2 Page 4 of 7
1

for ( initialization; condition; updateExpression ) {


statement(s);
}
statement(s); // you can add more here
}

The flowchart for the nested FOR loop will be:

Nested FOR Loop Algorithm:


1. Execute outer loop initialization
2. Check outer loop condition
a. If true proceed to step 3
b. If false proceed to step 9
3. Execute inner loop initialization
4. Check inner loop condition
a. If true proceed to step 5
b. If false proceed to step 7
5. Execute inner loop statements
6. Execute inner loop update expression
a. Go back to step 4
7. Execute outer loop other statements (if there is)
8. Execute outer loop update expression
a. Go back to step 2
9. End

LG-UBALDO-CS2-5.4.1.1 CS 2 Page 5 of 7
Example 1:

#include <iostream
using namespace std

int main (

for(int i = 0; i < 3; i++

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 =

NAVIGATE Time Allotment: 5 minutes

Let us check your knowledge.

Answer each question as briefly and concisely.

1. Explain the flow of control for a nested WHILE loop.


2. How are each statement in a nested FOR loop statement executed? Provide an
example.

Note: This is a non-graded assessment

LG-UBALDO-CS2-5.4.1.1 CS 2 Page 6 of 7

>

KNOT Time Allotment: 5 minutes

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 Loop in C++. (2002). Retrieved from Educba: https://www.educba.com/nested-loop-in-c-plus-


plus/

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

You might also like