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

Nested Loop: Muhammad Ahmad Lecturer Cs Department

The document discusses nested loops. It provides examples of using nested for loops to print patterns like numbers, stars, and averages of student quiz scores. It also discusses what makes a bad program and provides practice problems for students to write programs using various loop structures like for, while, and do-while loops, including nested loops. The goal is to help students understand and practice different types of loops and nested loops through worked examples and programming exercises.

Uploaded by

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

Nested Loop: Muhammad Ahmad Lecturer Cs Department

The document discusses nested loops. It provides examples of using nested for loops to print patterns like numbers, stars, and averages of student quiz scores. It also discusses what makes a bad program and provides practice problems for students to write programs using various loop structures like for, while, and do-while loops, including nested loops. The goal is to help students understand and practice different types of loops and nested loops through worked examples and programming exercises.

Uploaded by

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

Lecture 14:

Nested Loop

MUHAMMAD AHMAD
LECTURER
CS DEPARTMENT
Loops 2

 Nested Loops
Nested Loop 3

A loop within an other loop is called nested loop.

 int main()
 {
 for(int i=1; i<=5; i++)
Output
 { 1
 for(int j=1; j<=i; j++) 12
123
 {
1234
 cout<<j; 12345
 }
 cout<<"\n";
 }
 return 0;
 }
Nested Loop 4

 int main()
 {
 for(int i=5; i>=1; i--)
 {
 for(int j=i; j>=1; j--) Output
 { 54321
4321
 cout<<j;
321
 } 21
 cout<<"\n"; 1
 }
 return 0;
 }
Nested Loop 5

 int main()
 {
 for(int i=5; i>=1; i--)
 {
 for(int j=1; j<=i; j++)
 { Output
12345
 cout<<j; 1234
 } 123
12
 cout<<"\n";
1
 }
 return 0;
 }
Nested Loop 6

 int i=5;
 while(i>=1)
 {
 int j=1;
 while(j<=i)
 {
Output
 Cout<<j; 12345
 j++; 1234
123
 }
12
 i--; 1
 Cout<<“\n”;
}
Nested Loop 7

 for(int i=5;i>=1;i--)
 {
 for(int j=1;j<=i;j++)
 {
 Cout<<“*”;
 } Output
*****
 Cout<<“\n”;
****
} ***
**
*
8

Output
*
**
***
****
*****
Now lets try a real world example of nested 9
for loop.

 Suppose there are 10 students, you need to enter record of 5 quizzes


marks against each students. Then, their result is averaged and
displayed separately.
 For example:
 Enter student 1 marks
 Enter Marks of Quiz 1: 10
 Enter Marks of Quiz 2: 9
 Enter Marks of Quiz 3: 5
 Enter Marks of Quiz 4: 6
 Enter Marks of Quiz 5: 8
 Average Marks of Student 1 = 7.6
 Enter student 2 marks
By using Single Loop 10
By using nested loop 11
What makes a bad program? 12

 Repeating trial and error without


understanding the problem
 Writing Code without detailed analysis
and design (Abstraction, Algo)
 Writing tricky and dirty programs
Programms: 13

 Write a program using for, while and do while loops to:


 Display numbers from 0 to 100
 Display numbers from 100 to 0
 Display even numbers from 0 to 100
 Display odd numbers from 0 to 100
 Display even numbers from 100 to 0
 Display odd numbers from 100 to 0
 Display Square of numbers from 0 to 100
 Display Cube of numbers from 0 to 100
 Display Square of numbers from 100 to 0
 Display Cube of numbers from 100 to 0
 Display Square of numbers from 40 to 100
 Display Cube of numbers from 50 to 100
 Display Square of numbers from 500 to 1000
 Display Cube of numbers from 1000 to 1500
 Display Average of even numbers from 1000 to 1200
 Display Average of odd numbers from 1200 to 1000
Programs: 14

 Write a program using for, while and do while and nested loops to display the
following outputs.

12345678910 1
123456789 *
12 ======
12345678 ***
123 *********
1234567 *****
1234 *******
123456 *******
12345 *****
12345 *******
123456 ***
1234 *****
1234567 *
123 ***
12345678 ======
12 *
123456789
1 12345678910
15
Programs: 16

 Write a program using for, while and do while and nested loops to display the
following outputs.

13579 0 &
13579 02 && *
1357 024 &&& ***
1357 0246 &&&& *****
135 02468 &&&&& *******
135 0246810 &&&&& *****
13 024681012 &&&& ***
1 02468101214 &&& *
&&
&

You might also like