Chapter 3-Part 2
Control structures: Selection
• Algorithm Development (Section 3.1)
• Structured Programming(Section 3.2)
• Conditional Expressions (Section 3.3)
• Selection Statements: if statement
(Section 3.4)
• Selection Statements: switch statement
(Section 3.7)
1
The if - else statement
2-way branch of action
if(expression 1)
statement 1;
else
statement 2;
if(expression 2)
{
statement block 1
}
else
{
statement block 2
} 2
Practice : Identify the output of the following programs
3
The nested if-else
//example :
//k will be equal to 1 and
//m will be equal to 0
int x=9, y=7, k=0, m=0;
if(x > y)
k++;
else
m++;
4
The nested if-else
//example :
//k will be equal to 0 and
//m will be equal to 1
int x=5, y=7, k=0, m=0;
if(x > y)
k++;
else
m++;
5
Practice : Identify the output of the following programs
6
The nested if-else
//example :
//k will be equal to 0 and
//m will be equal to 0
//j will be equal to 1
int x=5, y=7, z=2, k=0, m=0, j=0;
if(x > y)
if(y < z)
k++;
else
m++;
else
j++; 7
The if – else if statement
multi-way branch of action
Form of nested if-else
if(expression1) if(expression1)
{ statement;}
{statement 1;} else
{
else if(expression2) if(expression2)
{ statement;}
{statement 2;} else
{
else if(expression3) if(expression3)
{ statement;}
{statement 3;} else
{
…… ……..
}
else }
{statement 4;} }
Each branch may contain a single statement or a block of statements 8
8
encloses within braces:{ statement; statement; statement;}
The if – else if statement (Example)
int day = 3;
if(day==1)
cout<<“Saturday\n”;
else if(day==2)
cout<<“Sunday\n”;
else if(day==3)
cout<<“Monday\n”;
else if(day==4)
cout<<“Tuesday\n”;
else if(day==5)
cout<<“Wednesday\n”;
else if(day==6)
cout<<“Thursday\n”;
else if(day==7)
cout<<“Friday\n”;
else 9
cout<<“Sorry! Invalid day number\n.”; 9
The if – else if statement (Example)
int grade = 81;
if(grade>=90)
cout<<“A”;
else if(grade>=80)
cout<<“B”;
else if(grade>=70)
cout<<“C”;
else if(grade>=60)
cout<<“D”;
else
cout<<“F”;
10
10
The if – else if statement (Example)
int rank = 3;
if (rank==1 || rank==2)
cout << "Lower division \n";
else if (rank==3 || rank==4)
cout << "Upper division \n";
else if (rank==5)
cout << "Graduate student \n";
else
cout << "Invalid rank \n";
11
11
Exercise 1
Continue this C++ program in order to check which is larger (x or y) and
display the larger number
12
Answer
13
Exercise 2
Continue this C++ program in order to check which is smaller (x or y) and
display the smaller number
14
Exercise 3
Continue this C++ program in order to check which is largest (x or y or z)
and display the largest number
15
Exercise 4
Continue this C++ program in order to check which is smallest (x or y or z)
and display the smallest number
16
Solve Practice in Page 126
In problems 1 through 7, draw a flowchart to perform
the steps indicated.
Then give the corresponding C++ statements.
Assume that the objects have been declared and
have reasonable values.
1. If time is greater than 15.0, increment time by 1
if (time > 15.0 )
++time;
17
Exercise
time = 10;
if (time > 15.0 )
++time;
What is the new value of time?
18
Solve Practice in Page 126
In problems 1 through 7, draw a flowchart to perform
the steps indicated.
Then give the corresponding C++ statements.
Assume that the objects have been declared and
have reasonable values.
1a. If time is greater than 15.0, increment time by 1
Why this answer is wrong
if (time > 15.0 );
++time;
19
Solve Practice in Page 126
2. When the square root of poly is less than 0.5, print
the value of poly.
if (sqrt(poly) < 0.5)
cout << poly;
What is the output of this program?
poly = 1;
if (sqrt(poly) < 0.5)
cout << poly;
20
Solve Practice in Page 126
3. If the difference between volt_1 and volt_2 is
larger than 10.0, print the values of volt_1 and
volt_2
if (abs(volt_1 – volt_2) > 10.0)
cout << volt_1 << ‘ ‘ << volt_2;
What is the output of this program?
volt_1 = 10;
volt_2 = 30;
if (abs(volt_1 – volt_2) > 10.0)
cout << volt_1 << ‘ ‘ << volt_2;
21
Solve Practice in Page 126
4. If the value of den is less than 0.05, set the result to zero;
otherwise, set the result equal to num divided by den.
if (den < 0.05)
result = 0;
else
result = num / den;
What is the new value of result after executing the
following statements?
int den = 0, num = 1;
if (den < 0.05) result = 0;
else result = num / den; 22
Solve Practice in Page 126
5. If the natural logarithm of x is greater than or equal
to 3, set time equal to zero and decrement count.
if (log(x) > = 3)
{
time = 0;
count--;
}
What are the new value of time and count after executing the
following statements?
float x=2.73, count = 0, time = 1;
if (log(x) > = 3) {
time = 0;
count--; } 23
Solve Practice in Page 126
6. If dist is less than 50.0 and time is greater than
10.0, increment time by 2; otherwise, increment
time by 2.5.
if (dist < 50.0 && time > 10.0)
time += 2;
else
time += 2.5;
24
Exercise
What are the new value of time after
executing the following statements?
float time = 0, dist = 10;
if (dist < 50.0 && time > 10.0)
time += 2;
else
time += 2.5;
25
Solve Practice in Page 126
7. If dist is greater than or equal to 100.0, increment time by 2,
If dist is between 50 and 100.0, increment time by 1;
otherwise, increment time by 0.5.
if (dist >= 100.0)
time += 2;
else if (dist < 100 && dist >= 50.0)
++time;
else
time = time + 0.5;
26
Exercise
What is the new value of time after executing
the following statements?
float time = 0, dist = 70;
if (dist >= 100.0)
time += 2;
else if (dist < 100 && dist >= 50.0)
++time;
else
time = time + 0.5; 27