Utline: Control Structures Conditions, Relational, and Logic Operators
Utline: Control Structures Conditions, Relational, and Logic Operators
Control Structures
Conditions, Relational, and Logic Operators
The if Statement and Flowchart
if with Compound Statements
Nested if statements
The switch Statement
Operator Precedence, Complementing a Condition
1
Common Programming Errors
CONTROL STRUCTURES
Control structure
Control the flow of execution in a program or a function
Repetition [Chapter 5]
}
CONDITIONS
Condition
Expression Value
'9' >= '0' 1 (true)
'a' < 'e' 1 (true)
'B' <= 'A' 0 (false)
'Z' == 'z' 0 (false)
'A' <= 'a' 1 (true)
ch >= 'a' && ch <= 'z' ch is lowercase? 9
ENGLISH CONDITIONS AS C EXPRESSIONS
English Condition Logical Expression
x and y are greater than z x > z && y > z
x is equal to 1 or 3 x == 1 || x == 3
10
NEXT . . .
Control Structures
Conditions, Relational, and Logic Operators
The if Statement and Flowchart
if with Compound Statements
Nested if statements
The switch Statement
Operator Precedence, Complementing a Condition
11
Common Programming Errors
if STATEMENT (ONE ALTERNATIVE)
if (condition) statementT ;
Example:
if (x != 0.0)
product = product * x ; 12
if STATEMENT (TWO ALTERNATIVES)
if (condition) statementT ;
else statementF ;
if condition evaluates to true then statementT is
executed and statementF is skipped; Otherwise,
statementT is skipped and statementF is executed
Example:
if (x >= 0.0) printf("Positive");
13
else printf("Negative");
FLOWCHARTS OF if STATEMENTS
if (x > 0)
num_pos = num_pos + 1;
else
if (x < 0)
num_neg = num_neg + 1;
else /* x equals 0 */
18
num_zero = num_zero + 1;
MULTIPLE-ALTERNATIVE DECISION FORM
The conditions are evaluated in sequence until a true
condition is reached
Ifa condition is true, the statement following it is
executed, and the rest is skipped
if (x > 0)
num_pos = num_pos + 1;
else if (x < 0) More
num_neg = num_neg + 1; Readable
else /* x equals 0 */
19
num_zero = num_zero + 1;
SEQUENCE OF if STATEMENTS
All conditions are always tested (none is skipped)
Less efficient than nested if for alternative decisions
if (x > 0)
num_pos = num_pos + 1; Less
if (x < 0) Efficient
num_neg = num_neg + 1; than
if (x == 0) nested if
num_zero = num_zero + 1; 20
IMPLEMENTING A DECISION TABLE
Use a multiple-alternative if statement to implement a
decision table that describes several alternatives
temperature
23
ROAD SIGN NESTED if STATEMENT
if (road_status == 'S')
if (temp > 0) {
printf("Wet roads ahead\n");
printf("Stopping time = 10 minutes\n");
} C associates else with the
else { most recent incomplete if
printf("Drive carefully!\n");
NEXT . . .
Control Structures
Conditions, Relational, and Logic Operators
The if Statement and Flowchart
if with Compound Statements
Nested if statements
The switch Statement
Operator Precedence, Complementing a Condition
25
Common Programming Errors
THE switch STATEMENT
Can be used to select one of several alternatives
Based on the value of a variable or simple expression
Variable or expression may be of type int or char
But not of type double
Example: Simple Calculator
User Input Operation
'+' result = a + b;
'–' result = a – b;
'*' result = a * b; 26
'/' result = a / b;
switch (op) { // op must be of type char
case '+':
result = a + b;
break; EXAMPLE OF A
case '-':
result = a – b; switch
break;
case '*': STATEMENT
result = a * b;
break;
case '/':
result = a / b;
break;
default:
printf("Error: unknown operation %c\n", op);
27
return; // to terminate the function
}
EXPLANATION OF switch STATEMENT
Ittakes the value of the character op and compares it to
each of the cases in a top down approach.
Itstops after it finds the first case that is equal to the
value of the variable op.
Itthen starts to execute each line following the matching
case till it finds a break statement.
Ifno case is equal to the value of op, then the default
case is executed.
default is optional. If no other case is equal to the value
of the controlling expression and there is no default case, 28
the entire switch body is skipped.
MORE ABOUT THE switch STATEMENT
One or more C statements may follow a case label.
You do not need to enclose multiple statements in curly
brackets after a case label.
You cannot use a string as a case label.
case "Add": is not allowed
Do not forget break at the end of each alternative.
If the break statement is omitted then execution falls through
into the next alternative.
29
33
SHORT-CIRCUIT EVALUATION
Stopping the evaluation of a logical expression as soon as
its value can be determined
Logical-OR expression of the form (a || b)
If a is true then (a || b) must be true, regardless of b
No need to evaluate b
However, if a is false then we should evaluate b