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

Lecture 10

Uploaded by

waleedsaghar7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lecture 10

Uploaded by

waleedsaghar7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

MIRPUR UNIVERSITY OF SCIENCE AND TECHNOLOGY

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

Q). Given a number n (e.g., n=6), draw this triangle.


Hints: Use three for loops; one to control number of rows, second to print empty
spaces and third to print *.
*
**
***
****
*****
******

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

You might also like