c programming language - selection
TRANSCRIPT
-
8/3/2019 C programming language - selection
1/43
Bilgisayar Mhendislii Blm
GIT Computer Engineering Department
Computer Programming
Selection Structures:
if and switch Statements
-
8/3/2019 C programming language - selection
2/43
GIT Computer Engineering Department 2
Control Structures
Controls the flow of program executionSequence
Selection
Repetition
We used sequence flowControl flows from one statement to next one
A compound statement in braces Ex: function body
We will learn selection control statements
ifswitch
They select one statement block and executes them
-
8/3/2019 C programming language - selection
3/43
GIT Computer Engineering Department 3
Conditions
We need conditions in selection structures
Ex: Testing the value of a variable
rest_heart_reate > 75
true (1): if greater than 75
false (0): otherwise
variable relational-operator constant
variable equality-operator constant
C accepts any nonzero value as a true
-
8/3/2019 C programming language - selection
4/43
GIT Computer Engineering Department 4
Relational and Equality Operators
-
8/3/2019 C programming language - selection
5/43
GIT Computer Engineering Department 5
Logical Operators
Used to form more complicated logicalexpressions
And (&&)Or (||)
Not (!)
Ex:salary < MIN_SALARY || dependents > 5
temperature > 90.0 && humidity > 0.90
n >= 0 && n = 0 && n
-
8/3/2019 C programming language - selection
6/43
GIT Computer Engineering Department 6
Operator Precedence
-
8/3/2019 C programming language - selection
7/43
GIT Computer Engineering Department 7
Evaluation for !flag || (y + z >= x - z)
-
8/3/2019 C programming language - selection
8/43
GIT Computer Engineering Department 8
Short-Circuit Evaluation
For logical && and || operations C evaluatesthe left operand first and right operand later
C stops evaluationIf the operation is && and left operand is false
Value of the expression is false
If the operation is || and left operand is true Value of the expression is true
-
8/3/2019 C programming language - selection
9/43
GIT Computer Engineering Department 9
Logical Expressions
min y
You can compare charactersa
-
8/3/2019 C programming language - selection
10/43
GIT Computer Engineering Department 10
Logical Assignment
Integers are used to represent logical valuesnon-zero value is true
zero is false
senior_citizen = (age >= 65);
not_senior_citizen = !senior_citizen;
male_senior_citizen = senior_citizen && gender == M;
is_letter = (a
-
8/3/2019 C programming language - selection
11/43
GIT Computer Engineering Department 11
The if statement
if statement is the primary selection structure Two alternatives
Selects one of two alternative statement blocks
if (rest_heart_rate > 56)printf(Keep up the exercise program! \n);
else
printf(You heart is in excellent health! \n);
One alternative
Executes the statement block or not
if (x != 0.0)product = product * x;
-
8/3/2019 C programming language - selection
12/43
GIT Computer Engineering Department 12
Flowcharts of if Statements
(a) Two Alternatives and (b) One Alternative
-
8/3/2019 C programming language - selection
13/43
GIT Computer Engineering Department 13
The if statement
if (condition) if (x > 0)
statement; printf(positive);
if (condition) if (x > 0)
statement; printf(positive);
else else
statement; printf(negative);
-
8/3/2019 C programming language - selection
14/43
GIT Computer Engineering Department 14
What is the output?
if age > 65printf(senior);
printf(citizen.\n);
-
8/3/2019 C programming language - selection
15/43
GIT Computer Engineering Department 15
What is the output?
if (age > 65);printf(senior);
printf(citizen.\n);
-
8/3/2019 C programming language - selection
16/43
GIT Computer Engineering Department 16
What is the output?
if (age > 65) {printf(senior);
printf(citizen.\n);
}
-
8/3/2019 C programming language - selection
17/43
GIT Computer Engineering Department 17
if statement with compound statements
if (condition) {
statements
}
if (condition) {
statements
}
else {
statements
}
if (radius > 0){
circ = 2*PI*radius;
printf(%f, circ);}
if (radius > 0) {circ = 2*PI*radius;
printf(%f, circ);
}
else {
printf(Radius is negative!..);
}
-
8/3/2019 C programming language - selection
18/43
GIT Computer Engineering Department 18
if Statement to Order x and y
-
8/3/2019 C programming language - selection
19/43
GIT Computer Engineering Department 19
Tracing an if statement
Hand trace = desk check
To verify the correctness
Step-by-step simulation of algorithm (orstatements) on paper
Use simple input values
Trace each caseTry inputs that cause the condition to be false and true
Execute each statement exactly as the computer
Dont assume the way of execution
Takes time
But saves time as well
-
8/3/2019 C programming language - selection
20/43
GIT Computer Engineering Department 20
Case Study: Simple Math Tool
Simple Math Tool to teach subtraction to a first gradestudent
Algorithm
1. Generate two single-digit integers randomly
number1 and number2 with number1 > number2
2. Display the question
such as What is 9 2?
3. Read students answer
4. Display a message indicating whether the answer iscorrect
-
8/3/2019 C programming language - selection
21/43
GIT Computer Engineering Department 21
Case Study: Water Bill Problem
Compute customers water bill
Demand charge = $35
Consumption charger = $1.10 per thousandgallons
Late charge for unpaid balance = $2
Inputs:Meter readings: previous, current
Unpaid balance
Outputs:
Water bill : use charge, late chage
-
8/3/2019 C programming language - selection
22/43
GIT Computer Engineering Department 22
Algorithm:1. Display user instructions
2. Get data
3. Compute use charge
4. Determine late charge
5. Figure bill amount6. Display the bill and charges
Functions Data requirements
Design and algorithm
-
8/3/2019 C programming language - selection
23/43
GIT Computer Engineering Department 23
Structure Chart for Water Bill Problem
-
8/3/2019 C programming language - selection
24/43
GIT Computer Engineering Department 24
-
8/3/2019 C programming language - selection
25/43
GIT Computer Engineering Department 25
-
8/3/2019 C programming language - selection
26/43
GIT Computer Engineering Department 26
-
8/3/2019 C programming language - selection
27/43
GIT Computer Engineering Department 27
-
8/3/2019 C programming language - selection
28/43
GIT Computer Engineering Department 28
Sample Run of Water Bill Program
-
8/3/2019 C programming language - selection
29/43
GIT Computer Engineering Department 29
Program Style
Consistent use of names in functionsUse same names to reference the same information
Ex: late_charge in three functions
They are all different variables but same information Cohesive functions
Each function should perform single operation
Easier to read, write, debug and maintainMore reusable
Use constant macrosCan be used anywhere in the same file
Statements are easier to understand (more descriptive)
Easier to maintain
-
8/3/2019 C programming language - selection
30/43
GIT Computer Engineering Department 30
Case Study: Water bill with conservation requirement
Modify the program
Conservation requirement: 5% decrease each
yearCharge twice if more than %95 of the last year
What changes are required?
-
8/3/2019 C programming language - selection
31/43
GIT Computer Engineering Department 31
Function comp_use_charge Revised
-
8/3/2019 C programming language - selection
32/43
GIT Computer Engineering Department 32
Nested if statements
if statement in another if statement
Used if there are more than one alternativedecisions
if (x > 0)
num_pos = num_pos + 1;
else
if (x < 0)
num_neg = num_neg + 1;
else
num_zero = num_zero + 1;
-
8/3/2019 C programming language - selection
33/43
GIT Computer Engineering Department 33
Alternative waysif (x > 0)
num_pos = num_pos + 1;
else
if (x < 0)
num_neg = num_neg + 1;
else
num_zero = num_zero + 1;
if (x > 0)
num_pos = num_pos + 1;
if (x < 0)
num_neg = num_neg + 1;
if (x == 0)
num_zero = num_zero + 1;
Less efficient
Less readable
-
8/3/2019 C programming language - selection
34/43
GIT Computer Engineering Department 34
Alternative ways
if (x > 0)
num_pos = num_pos + 1;
else
if (x < 0)
num_neg = num_neg + 1;
else
num_zero = num_zero + 1;
if (x > 0)
num_pos = num_pos + 1;
else if (x < 0)
num_neg = num_neg + 1;
else
num_zero = num_zero + 1;
Better way writing
-
8/3/2019 C programming language - selection
35/43
GIT Computer Engineering Department 35
Example: Payroll system
Compute tax amount for a salary
Decision table:
Salary Tax rate0 15,000 15
15,000 30,000 18
30,000 50,000 2250,000 80,000 27
80,000 150,000 33
-
8/3/2019 C programming language - selection
36/43
GIT Computer Engineering Department 36
Function comp_tax
Fl h f R d Si D i i
-
8/3/2019 C programming language - selection
37/43
GIT Computer Engineering Department 37
Flowchart of Road Sign Decision
-
8/3/2019 C programming language - selection
38/43
GIT Computer Engineering Department 38
if (road_status == S)
if (temp > 0) {
printf(wet road);
} else {
printf(icy road);
}
elseprintf(drive carefully);
if (road_status == S)
if (temp > 0) {
printf(wet road);
}
else
printf(drive carefully);
-
8/3/2019 C programming language - selection
39/43
GIT Computer Engineering Department 39
if (road_status == S)
if (temp > 0) {
printf(wet road);
} else {printf(icy road);
}
elseprintf(drive carefully);
if (road_status == S){
if (temp > 0) {
printf(wet road);
}} else
printf(drive carefully);
C associates an else with the most recent if statement
Use braces to force association
Th it h t t t
-
8/3/2019 C programming language - selection
40/43
GIT Computer Engineering Department 40
The switch statement
Select one of the several alternatives
Selection is based on the value of a singlevariable (of type int of char not double)
E l f it h St t t
-
8/3/2019 C programming language - selection
41/43
GIT Computer Engineering Department 41
Example of a switch Statement
The switch statement
-
8/3/2019 C programming language - selection
42/43
GIT Computer Engineering Department 42
The switch statement
switch (controlling expression) {label set1:
statements;break;
label set2:statements;break;
. . . .label setn:statements;break;
default:statements;
}
The switch statement
-
8/3/2019 C programming language - selection
43/43
GIT Computer Engineering Department 43
The switch statement
Statements following the matching case labelare executed until a break statement
After the break the rest of the switch statement isskipped
If no case label matches statements after the
default label are executed
The switch statement is more readable
Try to use default case