0% found this document useful (0 votes)
6 views19 pages

PF Lecture 3a

Uploaded by

almautulamreeka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views19 pages

PF Lecture 3a

Uploaded by

almautulamreeka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Programming

Fundamentals
Lecture 3a
Nested Loops
• In many situations, using a loop within another loop, called a nested loop, is
convenient. Here’s a simple example of a nested loop:
for(i = 1; i <= 5; i++) // start of outer loop
{
cout << "\ni is now " << i << endl;
for(j = 1; j <= 4; j++) // start of inner loop
cout << " j = " << j; // end of inner loop
} // end of outer loop
• The first loop, controlled by the value of i, is called the outer loop.
• The second loop, controlled by the value of j, is called the inner loop.
Nested Loops
• Notice that all statements in the inner loop are contained in the boundaries of
the outer loop, and a different variable is used to control each loop.
• For each trip through the outer loop, the inner loop runs through its entire
sequence.
• Therefore, each time the i counter increases by one, the inner for loop executes
completely, and goes through four values (j takes on the values 1 to 4), as
shown in Figure
This is the output of a sample run
i is now 1
j=1j=2j=3j=4
i is now 2
j=1j=2j=3j=4
i is now 3
j=1j=2j=3j=4
i is now 4
j=1j=2j=3j=4
i is now 5
j=1j=2j=3j=4
Example
Nested Loops
• To understand the usefulness of a nested loop, look at using one to compute then average
grade for each student in a class of 20 students.
• Each student has taken four exams during the semester.
• The final grade is calculated as the average of these exam grades.
• The pseudocode describing how to compute this average is as follows:
Nested Loops
• As described by the pseudocode, an outer loop consisting of 20
passes is used to compute the average grade for each student.
• The inner loop consists of four passes, and one examination grade is
entered in each inner loop pass.
• As each grade is entered, it’s added to the total for the student, and
at the end of the loop, the average is calculated and displayed.
• Because both the outer and inner loops are fixed-count loops of 20
and 4, respectively, for statements are used to create these loops.
Nested
For Loop
Nested
While Loop
Nested
Do While
Loop
Patterns using Nested Loops
Program to Print a Half-
Pyramid Using *
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; ++i) {
for(int j = 1; j <= i; ++j) {
cout << "* ";
}
cout << "\n";
}
return 0;
}
Patterns using Nested Loops
Program to Print a Half-Pyramid
Using Numbers
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; ++i) {
for(int j = 1; j <= i; ++j) {
cout << j << " ";
}
cout << "\n";
}
return 0;
}
Patterns using Nested Loops
Program to Print Half-Pyramid Using
Alphabets
#include <iostream>
using namespace std;
int main() {
char input, alphabet = 'A';
cout << "Enter the uppercase character you want to
print in the last row: ";
cin >> input;
// convert input character to uppercase
input = toupper(input);
for(int i = 1; i <= (input-'A'+1); ++i) {
for(int j = 1; j <= i; ++j) {
cout << alphabet << " ";
}
++alphabet;
cout << endl;
}
return 0;
}
Patterns using Nested Loops
Program to print Inverted Half-
Pyramid Using *
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = rows; i >= 1; --i) {
for(int j = 1; j <= i; ++j) {
cout << "* ";
}
cout << endl;
}
return 0;
}
Patterns using Nested Loops
Program to Print a Full Pyramid
Using *
#include <iostream>
using namespace std;
int main() {
int space, rows;
cout <<"Enter number of rows: ";
cin >> rows;
for(int i = 1, k = 0; i <= rows; ++i, k = 0) {
for(space = 1; space <= rows-i; ++space) {
cout <<" ";
}
while(k != 2*i-1) {
cout << "* ";
++k;
}
cout << endl;
}
return 0;
}
Program to Print a Pyramid Using Numbers
#include <iostream>
using namespace std;
while(k != 2*i-1) {
int main() {
if (count <= rows-1) {
int rows, count = 0, count1 = 0, k = 0;
cout << i+k << " ";
cout << "Enter number of rows: ";
++count;
cin >> rows;
}
for(int i = 1; i <= rows; ++i) {
else {
for(int space = 1; space <= rows-i; +
++count1;
+space) {
cout << i+k-2*count1 << " ";
cout << " ";
}
++count;
++k;
}
}
count1 = count = k = 0;
cout << endl;
}
return 0;
Patterns using Nested Loops
Program to Print Inverted Full
Pyramid Using *
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = rows; i >= 1; --i) {
for(int space = 0; space < rows-i; ++space)
cout << " ";
for(int j = i; j <= 2*i-1; ++j)
cout << "* ";
for(int j = 0; j < i-1; ++j)
cout << "* ";
cout << endl;
}
return 0;
}

You might also like