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

Today We Will Cover: For Loop Nested Loop

1. The document covers for loops and nested loops. It provides examples of using for loops to iterate through blocks of code a specified number of times. It also shows how nested loops can be used to iterate loops within other loops. 2. Examples are given of printing patterns using nested for loops, such as printing numbers or symbols in a pyramid shape. 3. Real world problems are presented that can be solved using nested for loops, such as averaging quiz scores for multiple students across multiple quizzes.

Uploaded by

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

Today We Will Cover: For Loop Nested Loop

1. The document covers for loops and nested loops. It provides examples of using for loops to iterate through blocks of code a specified number of times. It also shows how nested loops can be used to iterate loops within other loops. 2. Examples are given of printing patterns using nested for loops, such as printing numbers or symbols in a pyramid shape. 3. Real world problems are presented that can be solved using nested for loops, such as averaging quiz scores for multiple students across multiple quizzes.

Uploaded by

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

Today we will cover 1

 For Loop
 Nested Loop
For Loop 2

When you know exactly how many times you want to loop through a block of
code, use the for loop instead of a while loop:

Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been
executed
For Loop 3
Example

Example explained
Statement 1 sets a variable before the loop starts (int i = 0).
Statement 2 defines the condition for the loop to run (i must be less than 5). If the
condition is true, the loop will start over again, if it is false, the loop will end.
Statement 3 increases a value (i++) each time the code block in the loop has been
executed
For Loop 4
Another Example

Output ???
Task 5

 Print your name 10 times


Task 6

 Write a program to square first 10 numbers!


Printing Table 7
While & for loop comparison 8
Loops 9

 Nested Loops
Nested Loop 10

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 11

 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 12

 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 13

 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 14

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

Output
*
**
***
****
*****
Now lets try a real world example of nested 16
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 17
By using nested loop 18
What makes a bad program? 19

 Repeating trial and error without


understanding the problem
 Writing Code without detailed analysis
and design
 Writing tricky and dirty programs
Programms: 20

 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: 21

 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
22
Programs: 23

 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