Week 09 Lec 01 Updated
Week 09 Lec 01 Updated
Fundamentals
Lecture # 16
Monday, October 14, 2024
FALL 2024
FAST – NUCES, Faisalabad Campus
Rizwan Ul Haq
2 Nested Control Structures (Nested Loops)
A nested loop is a loop inside the body of another loop
Example:
outer loop
//termination phase
cout << "Passes = " << passes << endl;
cout << "Failures = " << failures << endl;
return(0);
}
Infinite loop
Infinite loop
int i = 0, value = 0;
while (i <= 20)
{
if (i % 2 == 0 && i <= 10)
value = value + i * i;
else if (i % 2 == 0 && i > 10)
value = value + i;
else
value = value - i;
i = i + 1;
CS1002 - Fall 2024 }
cout << "value = " << value << endl;
23 What is the output
int num = 12;
while (num >= 0)
{
if (num % 5 == 0)
{
num++;
continue;
}
cout << num << " ";
num = num - 2;
}
cout << endl;
#include <iostream>
using namespace std;
int main()
{
int x, y, z;
x = 4; y = 5;
z = y + 6;
do
{
cout << z << " ";
z = z + 7;
}
while (((z - x) % 4) != 0);
cout << endl;
return 0;
}
CS1002 - Fall 2024
25
a. int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= 5; ++j)
cout << setw(3) << i;
cout << endl;
}
b. int i, j;
for (i = 1; i <= 5; i++)
{
for (j = (i + 1); j <= 5; ++j)
cout << setw(3) << j;
cout << endl;
}
int main()
{
for (int i = 1; i <= 3; ++i)
{
for (int j = 1; j <= 3; ++j)
{
for (int k = 1; k <= 4; ++k)
cout << '*’;
cout << endl;
} // end inner for
c. int i, j;
for (i = 1; i <= 9; ++i)
{
for (j = 1; j <= (9 - i); ++j)
cout << " ";