Lecture 10
Lecture 10
DEPARTMENT OF CS&IT
Nested for Loop
(lecture # 10)
Farzana Riaz
(Lecturer)
Lecture Content
1. Loops
• Nested for loop
• Nested for loop: Example
Programming Fundamental 3
Nested for Loop: Example
• If a loop exists inside the body of another loop, it's called a nested loop
• The placing of one loop inside the body of another loop is called nesting. When
you "nest" two loops, the outer loop takes control of the number of complete
repetitions of the inner loop. While all types of loops may be nested, the most
commonly nested loops are for loops.
Programming Fundamental 4
Nested for Loop: Example
// Given a number n (e.g., n=6), draw this triangle
*
**
***
****
*****
******
Programming Fundamental 5
Nested for Loop: Example
// Given a number n (e.g., n=6), draw this triangle.
int main()
{
int n;
cout << ‘‘ Enter the value of n: ’’ << endl;
cin >> n;
for (int i=1; i<=n; i++) { // print rows
cout<<endl;
}
return 0;
}
Programming Fundamental 6
Nested for loop: Example
// Given a number n (e.g., n=6), draw this triangle.
int main()
{
i j n
int n;
cout << ‘‘ Enter the value of n: ’’ << endl; 3 3 6
cin >> n;
for (int i=1; i<=n; i++) { // print rows
cou<<endl;
for (int j=1; j<=i; j++) { // print *
cout<<‘*’; *
**
}
return 0;
}
Programming Fundamental 7
Nested for Loop: Example
Programming Fundamental 8
REFERENCES
1. Deitel, Paul J., and Harvey M. Deitel. "C++: how to program/Paul Deitel,
Harvey Deitel." (2012). (Chapter 5)
2. Zak, Diane. Introduction to Programming with C++. Cengage Learning, 2013.
9
Programming Fundamental
THANKS