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

10-lecture-10-nested-loop

This document covers nested loops in C++ programming, explaining their structure and providing syntax examples for nested for, while, and do...while loops. It includes practical coding exercises, such as generating multiplication tables and specific patterns using nested loops. Additionally, it outlines assignment guidelines for students, emphasizing originality and submission requirements.

Uploaded by

iremischyro
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

10-lecture-10-nested-loop

This document covers nested loops in C++ programming, explaining their structure and providing syntax examples for nested for, while, and do...while loops. It includes practical coding exercises, such as generating multiplication tables and specific patterns using nested loops. Additionally, it outlines assignment guidelines for students, emphasizing originality and submission requirements.

Uploaded by

iremischyro
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

ment of Computer Science & Engi

Air University

Intro to Programming
Week # 6
Repetition Structure
Lecture # 10

By: Saqib Rasheed


.

Nested loops
Nested Loops

• A loop can be nested inside of another loop.

• C++ allows at least 256 levels of nesting

• When working with nested loops, the outer


loop changes only after the inner loop is
completely finished
The syntax for a nested for
loop statement in C++
for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
statement(s); // you can put more statements.
}
The syntax for a nested while
loop statement in C++
while(condition)
{
while(condition)
{
statement(s);
}
statement(s); // you can put more statements.
}
The syntax for a nested
do...while loop statement in C++
do {
statement(s); // you can put more statements.
do {
statement(s);
}
while( condition );
}
while( condition );
For loop nesting
Nested loops (loop in loop) b
*************
cin >> a >> b; a *************
*************
for (int i = 0; i < a; i++)
{
*************
for (int j=0; j<b; j++)
{
cout << “*”;
}
cout << endl;
}

8
b
Nested loops (2)
*
a **
***
int a,b;
****
cin >> a >> b;

for (int i = 0; i < a; i++)


{
for (int j=0; j<b; j++)
{
if (j > i)
break;
cout << “*”;
}
cout << endl;
}
9
b
Nested loops (3)
*
a **
***
****

int a,b;
if (j > i) break;
cin >> a >> b;

for (int i = 0; i < a; i++)


{ <=i;
for (int j=0; j<b && j < i; j++)
{
cout << “*”;
}
cout << endl;
}
10
b
Nested loops (4)
*************
a ************
***********
int a,b;
**********
cin >> a >> b;

for (int i = 0; i < a; i++)


{
for (int j=0; j<b; j++)
{
if (j < i)
cout << “ ”;
else
cout << “*”;
}
cout << endl;
} 11
.

Write a program in C++ that prints a tables


Starting from 1 12.

1 2 3 4 5 6 7 8 9 10 11 12
2 4 6 8 10 12 14 16 18 20 22 24
3 6 9 12 15 18 21 24 27 30 33 36
4 8 12 16 20 24 28 32 36 40 44 48
5 10 15 20 25 30 35 40 45 50 55 60
6 12 18 24 30 36 42 48 54 60 66 72
7 14 21 28 35 42 49 56 63 70 77 84
8 16 24 32 40 48 56 64 72 80 88 96
9 18 27 36 45 54 63 72 81 90 99 108
10 20 30 40 50 60 70 80 90 100 110 120
11 22 33 44 55 66 77 88 99 110 121 132
12 24 36 48 60 72 84 96 108 120 132 144

Air University
#include <iomanip> // defines setw()
#include <iostream> // defines cout
.

using namespace std;


int main()
{
for (int x=1; x <= 12; x++)
{
for (int y=1; y <= 12; y++)
cout << setw(4) << x*y;
cout << endl;
} return 0; }
Air University
Assignment
Due next Class
Copied Assignments will be marked zero
Late Assignment not accepted
Hard Copies only
Write your name roll #, Section

14
Nested loops (5)

*
***
*****
*******
*********
***********

15
Develop a code in C++ that generate the
.

following series .Use nested while loop!

Series

No. 1 No. 2
No. 3
1 1 1
12 22 23
123 333 456
1234 4444 7 8 9 10
Air University

You might also like