Chapter 05
Chapter 05
IF it is raining
THEN I will not go outside
day is 6
(day == 0) || (day == 6)
0 (false) || (day == 6)
0 || 1 (true)
1 (true)
Short-Circuit Aspect
• && and || are short-circuit operators - if the
first operand determines the value, the
second operand is not evaluated
• first operand of && is 0, result is 0 without
evaluating second operand
• first operand of || is 1, result is 1 without
evaluating second operand
• second operand evaluated when needed
Negation
• Arithmetic negation:
- (4 *A* C)
Operator minus (-) negates numeric value
• Logical negation
Logical operator (!) negates logical value
E ! E
1 (true) 0 (false)
0 (false) 1 (true)
Example: ! (3 > 4)
! 0 (false)
1 (true)
Complex Expressions
Expressions can be built using arithmetic,
relational and logical operators
!((-5 >= -6.2) || (7 != 3) && (6 == (3 + 3)))
Logical true
statement2
Expression
false
statement3
Flow Chart for our Problem
input Num1
input Num2
true
Num2 > Num1 Store Num2 in Big
false
Code for Solution
int findMax(int Num1, int Num2) {
int Big;
Big = Num1;
if (Num2 > Num1) Big = Num2;
return Big;
}
Trace of Solution
Trace of findMax(17,53):
Logical true
Expression statement2
statement3
statement4
false
statement5
> 1 Stmt in an If
Does this work?
Statement1;
If (LogicalExpr)
Statement2;
Statement3; P
Statement4;
Statement5;
No, the indenting is irrelevant, section P is:
If (LogicalExpr)
Statement2;
Statement3;
Statement4;
> 1 Stmt in an If (A solution)
Solution - use a compound statement:
Statement1;
If (LogicalExpr) {
Statement2;
Statement3;
Statement4;
}
Statement5;
Statement in If
Any statement reasonable as the single
statement in if
– expression statement
• assignment statement
• function call (printf, scanf, etc.)
– compound statement
– if statement
Two-Way Selection
• Sometimes desirable for statement to be executed
when Logical Expression false
statement1
statement4
Two-Way Selection with If
statement
1
• Possible to use
combinations of if: Logical true
statement2
Expression
Statement1;
if (LogicalExpr) false
Statement2;
if (!(LogicalExpr)) true
! Logical
Statement3; Expression
statement3
Statement4;
• But not efficient false
statement4
If-Else Statement
• Syntax:
if (LogicalExpr)
StatementA
else
StatementB
if (LogicalExpr) {
/* statement(s) */
}
else {
/* statement(s) */
}
• Easy to add statements later
Programming Tip: Indenting
• Use indenting to make code more clear
– indent statements in function definition to
clearly identify body of function
– indent statement(s) executed for if by fixed
amount (2,3 chars) every time
– for ifs within an if indent further
– indent else statements(s) similarly
– may want to make {,} match (on separate lines)
Programming Tip: Conditions
first
Multiway Selection
• Multiway if more than 2 alternatives
• Example:
Student Score Message
0-55 Failing
56-65 Unsatisfactory
66-100 Satisfactory
• If-Else has two alternatives, to do multiway,
string together If-Else statements
Multiway Flow Chart
Score true
Failing
<= 55
false
Score true
Unsatisfactory
<= 65
false
Satisfactory
Multiway with If-Else
if (score <= 55)
printf(“Failing\n”);
else
if (score <= 65)
printf(“Unsatisfactory\n”);
else
printf(“Satisfactory\n”);
Indenting If-Else Combinations
• Multiway if-else statements sometimes
indented:
if (score <= 55)
printf(“Failing\n”);
else if (score <= 65)
printf(“Unsatisfactory\n”);
else
printf(“Satisfactory\n”);
• Rule for else: else matches most recent if
Conditions Checked in Reverse
Score true
Satisfactory
> 65
false
Score true
Unsatisfactory
> 55
false
Failing
Ordering Conditions
if (score > 65)
printf(“Satisfactory\n”);
else if (score > 55)
printf(“Unsatisfactory\n”);
else
printf(“Failing\n”);
• But must check conditions in correct order
if (score > 55)
printf(“Unsatisfactory\n”);
else if (score > 65)
printf(“Satisfactory\n”);
else
printf(“Failing\n”);
• Score of 70 would produce “Unsatisfactory”
Multiway with If Statements
• Possible but inefficient:
if (score <= 55)
printf(“Failing\n”);
if ((score > 55) && (score <= 65))
printf(“Unsatisfactory\n”);
if (score > 65)
printf(“Satisfactory\n”);
Program Robustness
• Example assumes score in interval [0,100]
but doesn’t check
• Add test:
if ((score >= 0) && (score <= 100))
/* print message */
else
printf(“Bad score: %d\n”,score);
Completed Code
if ((score >= 0) && (score <= 100)) {
if (score <= 55)
printf(“Failing\n”);
else if (score <= 65)
printf(“Unsatisfactory\n”);
else
printf(“Satisfactory\n”);
}
else
printf(“Bad score: %d\n”,score);
Nesting Example
if (A > 0) false
A>0
true
if ((A % 2) == 0)
S1 = S1 + A;
(A % 2)
else A == 0
== 0
S2 = S2 + A; true
false true false
else NS = S2 = S1 =
printf
if (A == 0) NS + A S2 + A S1 + A
printf(“A zero\n”);
printf("All done")
else
NS = NS + A;
printf(“All done.\n”);
Matching Else Example
false true
X<0
Y<0
true
false
Increment Increment
NegCount PosCount
Matching Single Else
• Easy to make mistake
if (X < 0)
if (Y < 0)
PosCount++;
else
NegCount++;
• Despite indenting, else matches wrong if
• PosCount updated when X < 0, Y >= 0
Matching Single Else Solutions
if (X < 0) if (X < 0) {
if (Y < 0) if (Y < 0)
PosCount++;
PosCount++; } /* compound stmt */
else else
; /* empty */ NegCount++;
else
if (X >= 0)
NegCount++;
NegCount++;
else
if (Y < 0)
PosCount++;
Choosing Conditions
Selection Example C Statement
One way X>0 If
Two way X>0 If-Else
X <= 0
Multiway 0 <= X <= 10 Nested
11 <= X <= 20 If-Elses
21 <= X <= 30
Another Multiway Method
• Consider another grading
if ((score == 9) ||
problem:
(score == 10))
Score Grade
grade = ‘A’;
9-10 A
else if ((score == 7) ||
7-8 B (score == 8))
5-6 C grade = ‘B’;
0-4 F else if ((score == 5) ||
(score == 6))
grade = ‘C’;
else
grade = ‘F’;
Switch Statement
• More readable approach: switch statement
switch (score) {
case 9: case 10:
grade = ‘A’;
break;
case 7: case 8:
grade = ‘B’;
break;
case 5: case 6:
grade = ‘C’;
break;
case 0: case 1: case 2: case3: case 4:
grade = ‘F’;
}
Switch Format
switch (Expression) {
case const1-1: case const1-2: ...
statement
statement
...
case const2-1: case const2-2: ...
statement
statement
...
default: ...
statement
statement
...
}
Switch Rules
• Expression must be integral type (no float)
• Case labels must be constant values
• No two case labels with same value
• Two case labels can be associated with
same set of statements
• Default label is not required
• At most one default label
Switch Rules
Evaluating
– determine value of expression
– look for corresponding label
– if no corresponding label look for default
– if no corresponding label or default, do nothing
– execute all statements from label to }