You are on page 1of 24

Introduction to Computer Science (C++)

13th - 14th Lecture

CONTROL STRUCTURES 1
(SELECTION)

1
Selection: if and if...else

• if and if...else statements can be used


to create:
− One-way selection
− Two-way selection
− Multiple selections

2
One-Way Selection

• One-way selection syntax:

• Statement is executed if the value of the


expression is true
• Statement is bypassed if the value is false;
program goes to the next statement
• Expression is called a decision maker
3
One-Way Selection (cont’d.)

4
Two-Way Selection

• Two-way selection syntax:

• If expression is true, statement1 is


executed; otherwise, statement2 is
executed
− statement1 and statement2 are any C++
statements
5
Two-Way Selection (cont’d.)

6
Compound (Block of) Statements

• Compound statement (block of statements):

• A compound statement functions like a single


statement
7
Compound (Block of) Statements
(cont’d.)
if (age > 18)
{
cout << "Eligible to vote." << endl;
cout << "No longer a minor." << endl;
}
else
{
cout << "Not eligible to vote." << endl;
cout << "Still a minor." << endl;
}

8
Multiple Selections: Nested if

• Nesting: one control statement is located


within another
• An else is associated with the most recent
if that has not been paired with an else

9
Multiple Selections: Nested if
(cont’d.)

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


Comparing if…else Statements
with a Series of if Statements

11
Comparing if…else Statements
with if Statements (cont’d.)

12
Conditional Operator (?:)

• Conditional operator (?:)


− Ternary operator: takes 3 arguments
• Syntax for the conditional operator:
expression1 ? expression2 : expression3
• If expression1 is true, the result of the
conditional expression is expression2
− Otherwise, the result is expression3
• Example: max = (a >= b) ? a : b;

13
switch Structures

• switch structure: alternate


to if-else

• switch (integral) expression


is evaluated first

• Value of the expression determines


which corresponding action is taken

• Expression is sometimes
called the selector
14
switch Structures (cont’d.)

15
switch Structures (cont’d.)
• One or more statements may follow a case
label
• Braces are not needed to turn multiple
statements into a single compound
statement
• When a case value is matched, all
statements after it execute until a break is
encountered
• The break statement may or may not
appear after each statement
• switch, case, break, and default are
16

reserved words
switch Structures (cont’d.)

17
Programs

• Write a program to calculate the electricity


bill. Bill will be charged according to units
consumed. Billing will be based on following
conditions:
For 1st 100 units, price per unit is 7.6
For 101 to 300 units, price per unit is 12.2
For 301 to 500 units, price per unit is 15
For above 500 units, price per unit is 20

18
Programs

• Write a program that prompts the user to


input a number. The program should then
output the number and a message saying
whether the number is positive, negative, or
zero.
• Write a program that prompts the user to
input three numbers. The program should
then output the numbers in ascending order.

19
Programs

A box of cookies can hold 24 cookies, and a container can


hold 75 boxes of cookies. Write a program that prompts the
user to enter the total number of cookies, the number of
cookies in a box, and the number of cookie boxes in a
container. The program then outputs the number of boxes
and the number of containers to ship the cookies. Note that
each box must contain the specified number of cookies, and
each container must contain the specified number of boxes.
If the last box of cookies contains less than the number of
specified cookies, you can discard it and output the number
of leftover cookies. Similarly, if the last container contains
less than the number of specified boxes, you can discard it
and output the number of leftover boxes.
20
Programs

• Write a program that mimics a calculator. The


program should take as input two integers
and the operation to be performed. It should
then output the numbers, the operator, and
the result. (For division, if the denominator is
zero, output an appropriate message.) Some
sample outputs follow:
−3+4=7
− 13 * 5 = 65

21
Programs

Suppose that classStanding is a char variable, and gpa and dues


are double variables. Write a switch expression that assigns the
dues as following:
• If classStanding is 'f', the dues are $150.00;
• if classStanding is 's‘ (if gpa is at least 3.75, the dues are
$75.00; otherwise, dues are 120.00);
• If classStanding is 'j' (if gpa is at least 3.75, the dues are $50.00;
• otherwise, dues are $100.00);
• if classStanding is 'n' (if gpa is at least 3.75, the dues are
$25.00; otherwise, dues are $75.00). (Note that the code
• 'f' stands for first year students, the code 's' stands for second
year students, the code 'j' stands for juniors, and the code 'n'
stands for seniors.)
22
23
Write a program that calculates the monthly paycheck of a
salesperson at a local department store.
Every salesperson has a base salary. The salesperson also receives
a bonus at the end of each month, based on the following criteria: If
the salesperson has been with the store for five years or less, the
bonus is $10 for each year that he or she has worked there. If the
salesperson has been with the store for more than five years, the
bonus is $20 for each year that he or she has worked there. The
salesperson can earn an additional bonus as follows: If the total sales
made by the salesperson for the month are at least $5,000 but less
than $10,000, he or she receives a 3% commission on the sale. If the
total sales made by the salesperson for the month are at least
$10,000, he or she receives a 6% commission on the sale.

You might also like