You are on page 1of 24

Programming

Lecture 9

Dr. Badria Nabil

Control Structures (Repetition)


1
Objectives
In this lecture, you will:
• Learn about repetition (looping) control
structures
• Discover how to form and use while loop
• Explore how to construct and use count-
controlled, and sentinel-controlled repetition
structures
• Discover how to form and use nested control
structures

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 2


Assignment 6

• Write a C++ program using switch statement


to take the month number from the user and
display the month name.

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 3


Assignment 6 Solution

4
Assignment 7
• Write a C++ program that takes the sales from the user then calculates
and display the commission amount.
• Commission amount = sales * commission rate
• The commission rate is depending on the value of sales as the
following table:

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 5


Assignment 7 Solution

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 6


Why Is Repetition Needed?

• Repetition allows you to efficiently use


variables
• Can input, add, and average multiple
numbers using a limited number of variables
• For example, to add five numbers:
− Declare a variable for each number, input the
numbers and add the variables together
− Create a loop that reads a number into a variable
and adds it to a variable that contains the sum of
the numbers
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 7
while Looping (Repetition)
Structure
• Repetition statement
− Action repeated while some condition remains true
• The general form of the while statement is:

• while loop repeats until condition becomes false


• while is a reserved word
• Statement can be simple or compound
• Expression acts as a decision maker and is usually a logical
expression
• Statement is called the body of the loop
• The parentheses are part of the syntax
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 8
while Looping (Repetition)
Structure (continued)

• Infinite loop: continues to execute endlessly


− Avoided by including statements in loop body
that assure exit condition is eventually false

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 9


while Looping (Repetition)
Structure (continued)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 10


Designing while Loops

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 11


Case 1: Counter-Controlled
while Loops
• If you know exactly how many pieces of data
need to be read, the while loop becomes a
counter-controlled loop

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 12


Counter-Controlled Repetition Example

• Problem statement
A class of ten students took a quiz. The grades
(integers in the range 0 to 100) for this quiz are
available to you. Calculate and display the total of all
student grades and the class average on the quiz.

13
1 Set total to zero
2 Set grade counter to one
3
4 While grade counter is less than or equal to ten
5 Prompt the user to enter the next grade
6 Input the next grade
7 Add the grade into the total
8 Add one to the grade counter
9
10 Set the class average to the total divided by ten
11 Print the total of the grades for all students in the class
12 Print the class average

Fig. 4.7 | Pseudocode algorithm that uses counter-controlled repetition to solve the
class average problem.

14
# include<iostream>
using namespace std;
int main ()
{
// declaration phase
int total;
int gradecounter;
int grade;
double average;
// initialization phase
total= 0;
gradecounter =1;
// loop until 10
while(gradecounter <= 10)
{
cout <<“enter grade :”;
cin >> grade;
total = total + grade;
gradecounter = gradecounter + 1;
}
average = total / 10;
cout << “\n Total of all 10 grades is” << total;
cout << “ \n class average is “<< average;
}
15
Outline

Welcome to the grade book for


CS101 C++ Programming
Enter grade: 67
Enter grade: 78
Enter grade: 89
Enter grade: 67
Enter grade: 87
Enter grade: 98
Enter grade: 93
Enter grade: 85
Enter grade: 82
Enter grade: 100
Total of all 10 grades is 846
Class average is 84

16
Case 2: Sentinel-Controlled
while Loops
• Sentinel variable is tested in the condition
and loop ends when sentinel is encountered

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 17


Sentinel-Controlled Repetition

• Problem statement
Develop a class average program that
processes grades for a number of students
each time it is run.

18
1 Initialize total to zero
2 Initialize counter to zero
3
4 Prompt the user to enter the first grade
5 Input the first grade (possibly the sentinel)
6
7 While the user has not yet entered the sentinel
8 Add this grade into the running total
9 Add one to the grade counter
10 Prompt the user to enter the next grade
11 Input the next grade (possibly the sentinel)
12
13 If the counter is not equal to zero
14 Set the average to the total divided by the counter
15 Print the total of the grades for all students in the class
16 Print the class average
17 else
18 Print “No grades were entered”

| Class average problem pseudocode algorithm with sentinel-


Fig. 4.11

controlled repetition.
19
# include <iostream>
Using namespace std;
int main ()
{
// declaration phase
int total;
int gradecounter;
int grade;
double average;
// initialization phase
total= 0;
gradecounter =0;
// processing phase
cout <<“ enter grade or -1 to quit:”;
cin >> grade;

20
// loop until value read from user
While (grade != -1)
{
total = total + grade;
gradecounter = gradecounter + 1;
cout <<“ enter grade or -1 to quit:”;
cin >> grade;
}
// termination phase
if ( gradecounter != 0)
{
average = total / gradecounter;
cout << “ \n total of all “ << gradecounter<< “total pf the grade =“<< average;
cout <<“\n class average is”<< average;
}
Else
cout << “ no grades were entered”;

Return 0;
}
21
Outline

Welcome to the grade book for


CS101 C++ Programming

Enter grade or -1 to quit: 97


Enter grade or -1 to quit: 88
Enter grade or -1 to quit: 72
Enter grade or -1 to quit: -1

Total of all 3 grades entered is 257


Class average is 85.67

22
Assignment 8
A college offers a course that prepares students for the state exam for
real estate brokers. Last year, ten of the students who completed this
course took the exam. The college wants to know how well its students
did on the exam. You have been asked to write a program to summarize
the results. You have been given a list of these 10 students. Next to each
name is written a 1 if the student passed the exam or a 2 if the student
failed.
Your program should analyze the results of the exam as follows:
1. Input each test result (i.e., a 1 or a 2). Display the prompting message
“Enter result” each time the program requests another test result.
2. Count the number of test results of each type.
3. Display a summary of the test results indicating the number of
students who passed and the number who failed.
4. If more than eight students passed the exam, print the message
“Raise tuition.”

23
1 Initialize passes to zero
2 Initialize failures to zero
3 Initialize student counter to one
4
5 While student counter is less than or equal to 10
6 Prompt the user to enter the next exam result
7 Input the next exam result
8
9 If the student passed
10 Add one to passes
11 Else
12 Add one to failures
13
14 Add one to student counter
15
16 Print the number of passes
17 Print the number of failures
18
19 If more than eight students passed
20 Print “Raise tuition”

Fig. 4.15 | Pseudocode for examination-results problem.


24

You might also like