Computer Programming - # 08
Computer Programming - # 08
Computer Programming
Experiment No.8
Introduction to Nested Loops
Prepared for:
Mam Rania Muqadas
By:
Name: Ameer Hamza
ID: SU91-BBETM-F23-002
Section: A
Semester: 2nd Semester
Experiment No.8
Introduction to Nested Loops
(Rubrics)
A. PSYCHOMOTOR
Serial Criteria Allocated Unacceptable Poor Fair Good Excellent Total
No. Marks 0% 25% 50% 75% 100% Obtained
B. AFFECTIVE
2
Computer Programming Lab 8
CONTENTS OF LAB 8
1 Objectives..............................................................................................................................................4
2 Introduction to Nested Loops.................................................................................................................4
2.1 Types of Nested Loops..................................................................................................................4
2.2 Syntax For Nested Loop................................................................................................................4
2.3 Nested-For-Loop............................................................................................................................5
2.4 Structure of For Loop.....................................................................................................................5
2.5 Nested For Loop Flowchart...........................................................................................................5
2.6 Example 1......................................................................................................................................6
3 Lab Tasks:..............................................................................................................................................7
Task 1:........................................................................................................................................................7
Task 2:........................................................................................................................................................7
4 Home Assignment:.................................................................................................................................7
Task 1:........................................................................................................................................................7
task 2:.........................................................................................................................................................8
3
Computer Programming Lab 8
1 OBJECTIVES
To get basic understanding of nested loop.
To practice how to use nested loop.
In this lab manual, we will only focus on Nested-For-Loop. Other Loops will be discussed in future labs.
Note: There can be mixed type of nested loop i.e. as for loop inside a while loop, or a while loop inside a
do-while loop.
Outer_loop
{
Inner_loop
{
// inner loop statements.
4
Computer Programming Lab 8
}
// outer loop statements.
}
Outer_loop and Inner_loop are the valid loops that can be a 'for' loop, 'while' loop or 'do-while' loop.
2.3 NESTED-FOR-LOOP
for (initialization; condition; update)
{
for (initialization; condition; update)
{
// inner loop statements.
}
// outer loop statements.
}
5
Computer Programming Lab 8
2.6 EXAMPLE 1
Code Output
#include <iostream> Statement of outer for loop
Using namespace std; 0 0 :Statement of inner for loop
int main() 0 1 :Statement of inner for loop
{ 0 2 :Statement of inner for loop
int i,j; 0 3 :Statement of inner for loop
for(i=0; i<=2; i++){ Statement of outer for loop
cout<<i<<"Statement of outer for loop\n"; 1 0 :Statement of inner for loop
for(j=0; j<=3; j++){ 1 1 :Statement of inner for loop
cout<<i<<j; 1 2 :Statement of inner for loop
cout<<": Statement of inner for loop\n"; 1 3 :Statement of inner for loop
} Statement of outer for loop
} 2 0 :Statement of inner for loop
return 0; 2 1 :Statement of inner for loop
} 2 2 :Statement of inner for loop
2 3 :Statement of inner for loop
6
Computer Programming Lab 8
3 LAB TASKS:
TASK 1:
Write a C++ program to display the multiplication tables up to a certain number.
TASK 2:
(Drawing Patterns with for Loops) Write a program that uses for statements to print the following patterns
separately, one below the other. Use for loops to generate the patterns. All asterisks (*) should be printed
by a single statement of the form cout<< '*'; (this causes the asterisks to print side by side).
Do programs by loop.
a) b) c) d)
4 HOME ASSIGNMENT:
TASK 1:
Write a C++ program to print a number pyramid pattern using nested loops.
7
Computer Programming Lab 8
1
12
123
1234
12345
TASK 2:
Print the following patterns using loop.
a) b)