CS2 5.4.2 Nested Loop
CS2 5.4.2 Nested Loop
Nested loop in C++ programming is identified as loop statement inside a loop statement.
Nested loops maybe in a form of a nested WHILE, FOR and DO-WHILE loop. The basic
syntax and flowchart for the nested WHILE loop are:
initialization;
while(condition
initialization
while(condition
statement(s)
updateExpression
statement(s)
updateExpression
While for the nested FOR loop, the syntax and flowchart is:
statement(s)
}
LG-UBALDO-CS2-5.4.1.2 CS 2 Page 1 of 4
{
Programming Exercise
Based on the example given on the previous module, create a program that replicated
the triangle pattern shown. Try to look at the examples closely and think of an algorithm
that replicates the outputs. Let’s see if you still look at Triangle problems the same way
after this problem.
Input:
Integer value n.
Output:
A triangle based on the inputted value of n.
Input Output
3 *
**
***
4 *
**
***
****
7 *
**
***
****
*****
Modified problem from: codechum.com
LG-UBALDO-CS2-5.4.1.2 CS 2 Page 2 of 4
KNOT Time Allotment: 5 minutes
A loop can have another loop inside and this structure is called nested loop. We can
implement a nested FOR, WHILE or DO-WHILE loops. We can put any kind of loop
inside any kind of loop.
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
LG-UBALDO-CS2-5.4.1.2 CS 2 Page 3 of 4
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.2 CS 2 Page 4 of 4
ti
ti
fi
ti
ti