0% found this document useful (0 votes)
21 views30 pages

Week 09 Lec 01 Updated

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

Week 09 Lec 01 Updated

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

CS1002 – Programming

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

for (row = 1; row <= 3; row++)


inner loop
{
for (col = 1; col <= 3; col++)
{
cout << row * col << endl;
}
}

CS1002 - Fall 2024


3 Nested Control Structures
 To create the following pattern:
*****
*****
*****
*****
*****

 We can use the following code:


//int i,j;
for (int i = 1; i <= 5 ; ++i)
{
for (int j = 1; j <= 5; ++j)
cout << "*";
cout << endl;
}

CS1002 - Fall 2024


4 Nested Control Structures
 To create the following pattern:
*
**
***
****
*****

 We can use the following code:


for (int i = 1; i <= 5 ; ++i)
{
for (int j = 1; j <= i; ++j)
cout << "*";
cout << endl;
}

CS1002 - Fall 2024


5 Nested Control Structures (cont'd.)
 What is the result if we replace the first for statement with the following?
for (int i = 5; i >= 1 ; i--)
{
for (int j = 1; j <= i; ++j)
cout << "*";
cout << endl;
}
Answer:
*****
****
***
**
*
CS1002 - Fall 2024
6 Example

for ( int i = 1; i <= 5; i++)


{
for( int j = 1; j <= 10; j++)
cout << setw(3) << i * j ;
cout << endl ;
}
CS1002 - Fall 2024
8 Output = ?
for (int y = 1; y <= 3; ++y) {
for (int x = 1; x <= 3; ++x) {
cout << x * y << " ";
}
cout << "\n";
}

CS1002 - Fall 2024


9 loop Structures
 Problem statement
A college has a list of test results (1 = pass, 2 = fail) for 10 students. Write a
program that analyzes the results. If more than 8 students pass, print “Raise
Tuition”.
 Notice that
 Program processes 10 results
Fixed number, use counter-controlled loop
 Two counters can be used
One counts number that passed
Another counts number that fail
 Each test result is 1 or 2
If not 1, assume 2

CS1002 - Fall 2024


10 loop Structures
 Top level outline
Analyze exam results and decide if tuition should be raised
 First refinement
Initialize variables
Input the ten quiz grades and count passes and failures
Print a summary of the exam results and decide if tuition should be raised
 Refine
Initialize variables
to
Initialize passes to zero
Initialize failures to zero
Initialize student counter to one

CS1002 - Fall 2024


11 loop Structures
 Refine
Input the ten quiz grades and count passes and failures
to
While student counter is less than or equal to ten
Input the next exam results
If the student passed
Add one to passes
Else
Add one to failures
Add one to student counter
CS1002 - Fall 2024
12 loop Structures
Refine
Print a summary of the exam results and decide if tuition should be
raised
to
Print the number of passes
Print the number of failures
If more than eight students passed
Print “Raise tuition”

CS1002 - Fall 2024


#include<iostream>
using namespace std;
int main()
13 {
int passes = 0; //number of passes
int failures = 0; //number of failures
int stdCounter = 1; //student counter
int result; //one exam result
//process 10 users using counter-controlled loop
while (stdCounter <= 10)
{
//prompt user for input and obtain value from user
cout << "Exam Result ( 1 = pass , 2 = fail): ";
cin >> result;

//if 1 increment passes


if (result == 1) //if-else nested in while loop
++passes;
else //if result not one increment failures
++failures;

//increment student counter so that loop can terminate


++stdCounter;
CS1002 - Fall 2024 } //end while
14

//termination phase
cout << "Passes = " << passes << endl;
cout << "Failures = " << failures << endl;

// if more than 8 students passed then raise tution


if (passes > 8)
cout << "Raise Tution" << endl;

return(0);
}

CS1002 - Fall 2024


15

CS1002 - Fall 2024


17 Debugging Loops
Loops are harder to debug than sequence and selection
structures
Use loop invariant
Set of statements that remains true each time the loop body is
executed
Most common error associated with loops is off-by-one

CS1002 - Fall 2024


18
Examples

CS1002 - Fall 2024


19 Example program
What is the out put of following code segment?
int count = 0;
while (count++ < 10)
cout << "This loop repeats statements." <<endl;

What is the out put of following code segment?


int count = 5;
while(--count > 0)
cout << count << " " ;
cout << endl ;

What is the out put of following code segment?


int count = 5 ;
while (count-- > 0)
cout << count << " " ;
cout << endl;

What is the out put of following code segment?


int count = 1 ;
while (count++ <= 5)
cout << count * (count - 2) << " " ;
cout << endl;
CS1002 - Fall 2024
20

Infinite loop

Infinite loop

CS1002 - Fall 2024


21

CS1002 - Fall 2024


Suppose that the input is 38 35 75 44 -1. What is the output of
the following code? Assume all variables are properly declared.
22 sum = 0;
cin >> num;
for (j = 0; j <= 3; j++)
{
cin >> num;
sum = sum + num;
}
cout << "Sum = " << sum << endl;

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;

CS1002 - Fall 2024


24

#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

CS1002 - Fall 2024


26

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;
}

CS1002 - Fall 2024


27

What will be the output of the following code

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

cout << endl;


} // end outer for
}
CS1002 - Fall 2024
a. int i, j;
for (i = 1; i <= 5; ++i)
{
28 for (j = 1; j <= i; ++j)
cout << setw(3) << j;
cout << endl;
}

b. const int M = 10;


const int N = 10;
int i, j;
for (i = 1; i <= M; ++i)
{
for (j = 1; j <= N; ++j)
cout << setw(3) << M * (i - 1) + j;
cout << endl;
}

c. int i, j;
for (i = 1; i <= 9; ++i)
{
for (j = 1; j <= (9 - i); ++j)
cout << " ";

for (j = 1; j <= i; ++j)


cout << setw(1) << j;

for (j = (i - 1); j >= 1; --j)


cout << setw(1) << j;
cout << endl;
CS1002 - Fall 2024 }
29 Summary
 C++ has three looping (repetition) structures:
 while, for, and do…while
 while, for, and do are reserved words
 while and for loops are called pretest loops
 do...while loop is called a posttest loop
 while and for may not execute at all, but do...while always executes
at least once

CS1002 - Fall 2024


30 Summary (cont'd.)
 while: expression is the decision maker, and the statement is the body
of the loop
 A while loop can be:
 Counter-controlled
 Sentinel-controlled
 Condition/Event Controlled
 Flag-controlled
 EOF-controlled
 In the Windows console environment, the end-of-file marker is
entered using Ctrl+z
CS1002 - Fall 2024
31 Summary (cont'd.)
 for loop: simplifies the writing of a counter-controlled while loop
 Putting a semicolon at the end of the for loop is a semantic error
 Executing a break statement in the body of a loop immediately
terminates the loop
 Executing a continue statement in the body of a loop skips to the
next iteration

CS1002 - Fall 2024


32 Questions

CS1002 - Fall 2024

You might also like