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

Computer Programming - # 08

This is the Manuel of computer programming
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Computer Programming - # 08

This is the Manuel of computer programming
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Department of Biomedical Engineering Technology

The Superior University, Lahore

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

Total Marks: _______10_________


Obtained Marks: __________________
Signature: __________________
Date: __________________
Computer Programming Lab 8

Experiment No.8
Introduction to Nested Loops
(Rubrics)

Name: Ameer Hamza ID: SU91-BBETM-F23-002

A. PSYCHOMOTOR
Serial Criteria Allocated Unacceptable Poor Fair Good Excellent Total
No. Marks 0% 25% 50% 75% 100% Obtained

1 Syntax 1 0 0.25 0.5 0.75 1


Handling
2 Program 2 0 0.5 1 1.5 2
Coding Skills
3 Accuracy in 3 0 0.75 1.5 2.25 3
Output Results
Sub-Total 6 Sub Total Marks Obtained in Psychomotor (P)

B. AFFECTIVE

Serial Criteria Allocated Unacceptable Poor Fair Good Excellent Total


No. Marks 0% 25% 50% 75% 100% Obtained

1 Assigned tasks 2 0 0.5 1 1.5 2


2 Lab Report 2 0 0.5 1 1.5 2
Sub-Total 4 Sub Total Marks Obtained in Affective (A)

Instructor Name: _______________ Total Marks (P+A): ______________

Instructor Signature: ______________ Obtained Marks (P+A): ______________

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

Introduction to Nested Loops

1 OBJECTIVES
 To get basic understanding of nested loop.
 To practice how to use nested loop.

2 INTRODUCTION TO NESTED LOOPS


A loop inside another loop is called a nested loop. The depth of nested loop depends on the complexity of
a problem. We can have any number of nested loops as required. Consider a nested loop where the outer
loop runs n times and consists of another loop inside it. The inner loop runs m times. Then, the total
number of times the inner loop runs during the program execution is n*m.

2.1 TYPES OF NESTED LOOPS


Types of
Nested Loops

Nested While Nested Do- Nested For


Loop While Loop Loop

Figure 3 Nested Loops

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.

2.2 SYNTAX FOR NESTED LOOP


C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements
inside another loop. Let's observe an example of nesting loops in C.
Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any
number of loops. The nesting level can be defined at n times. You can define any type of loop inside
another loop; for example, you can define 'while' loop inside a 'for' 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.
}

2.4 STRUCTURE OF FOR LOOP


The structure of Nested-For-Loop is shown in the given figure:

Figure 4: Structure of Nested For Loop

2.5 NESTED FOR LOOP FLOWCHART


Initially, nested for loop evaluates outer for loop test expression and evaluates only once. If the condition
is true, the flow of control jumps to the inner for loop.
Then, the loop evaluates the condition of the inner loop. when the condition is true, it executes codes of
inside the inner for loop. If the test expression returns false, the flow of control skips the execution and
jumps out to the outer loop for execution.
Then the outer for loop test expression is evaluated again when the test expression is false, the flow of
control skips the execution and come out of the loop for rest.

5
Computer Programming Lab 8

Figure 5: Flowchart of Nested-For-Loop

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)

You might also like