Professional Documents
Culture Documents
Thinking to Programming
Lesson 6
Decision Making
(Reference: Harry H. Cheng, Chapter 5)
6-2
Relational Operators
Is getchar() == '\n' ?
Does the integer n lie in the interval
6-3
[0,10]?
Relational Operators in C
x = -5;
printf("The expression x*x >= 0 has value
%d", x*x>=0);
Suppose a = 1, b = 5
printf("The value of a==b is %d", a==b);
a==b is False (0)
printf("The value of a=b is %d", a=b);
a=b = 5 6-7
Note:
In C, any non-zero numeric value can also
represent TRUE.
Example:
a = 15;
if (a) printf("%d", a);
Interpretation:
Return value of expression (a = 15) is 15.
Result of (a = 15) is TRUE (non-zero).
printf() will be executed.
6-9
Note:
What will be shown on screen after running:
int a = 15;
if (a = 0) printf("%d\n", a);
char ch='1';
if (isdigit(ch) != 0) //isdigit() is TRUE (non-
zero)
printf("%c is a digit.", ch);
else
printf("%c is not a digit.", ch);
Simpler Form:
6-12
Example
Expression Value
Symbols && || !
These are needed to form complex conditions.
They enhance the decision-making capabilities
of our C programs.
P Q P && Q P || Q !P
F F F F T
F T F T T
T F F T F
T T T T F
6-16
Logical Assignment
We may even assign return value (either 0 or 1)
of logical expressions evaluation to variables.
even = (n%2 == 0);
- (n%2 == 0)returns true (1) if n divided by 2
gives zero remainder
in_range = (n > -10 && n < 5);
- (n > -10 && n < 5) returns true (1) if n is
between -10 and 5, excluding -10 and 5
is_letter = ('A' <= ch && ch <= 'Z') || ('a'
<= ch && ch <= 'z');
- Above returns true (1) if ch is an alphabet
6-17
i.e. ch is within [A, Z] or [a, z]
More about the if statement
if (expression)
{
statement_1;
.......
statement_n;
}
a*x*x + b*x + c = 0
Program 2.4 (Slide 2-27 and 28) fails if the user
enters 0 for the coefficient a.
We should test for a non-zero value of a given by
the user.
if (a != 0) //can be written as if (a)
{ root1 = (-b + sqrt(b*b-4*a*c))/(2*a);
root2 = (-b - sqrt(b*b-4*a*c))/(2*a);
}
6-19
The else clause
The if structure is very frequently used with the
optional else:
if (expression)
{ . . . . . . .
}
else
{ . . . . . . .
}
If there is only 1 statement to be executed, {} can be
excluded i.e. we can just write
if (expression)
one_statement;
else 6-20
another_one_statement;
Program 6.2 (quadratic equation)
6-22
Nested if statements
6-23
Program 6.3 Brown if-else
statements
if (a == 0) { . . .} are NESTED
within Black
else if-else
{ statements
if (b*b-4*a*c < 0)
printf("Complex roots.\n")
else //2 real roots
{
root1 = (-b + sqrt() . . .);
. . .
}
6-24
}
Give it a Try Yourself!
Modify Program 6.3 so that it appears to be
capable of handling complex roots. How?
A little cheating!
Suppose the complex roots are
α + iβ, α - iβ
Compute α and β separately, and in the output
insert the strings “+ i” and “- i”.
6-25
if..else if..else if..
Program Fragment 6.4:
6-31
Program 6.5 (Continued)
char selection;
printf("Select form of Ohm’s Law.\n");
printf("[A] V, [B] I, [C] R \n");
printf("Your selection => ")
scanf("%c", &selection);
// user’s choice will be stored in
variable selection
6-32
Program 6.5 (Continued)
if (selection == 'A')
printf("V = I*R");
else if (selection == 'B')
printf("I = V/R");
else if (selection == 'C')
printf("R = V/I");
else
printf("Wrong selection");
6-33
The switch statement
Too many if-else statements can be confusing.
In this case, we may use the switch statement.
switch (expression)
{ case value_1: statement(s)
case value_2: statement(s)
. . . . . .
case value_n: statement(s)
default: statement(s)
} 6-34
The switch statement (Continued)
The expression MUST return an integer
(including char) value.
The computer compares this returned value
against the value following each case label, i.e.
value_1, value_2...
6-36
Program 6.6a (without ‘break’)
switch (selection)
{case 'A': printf("V = I*R");
//selection=‘B’ does not match ‘A’ so printf()is
not executed
case 'B': printf("I = V/R");
//selection=‘B’ matches ‘B’ so I = V/R is printed
case 'C': printf("R = V/I");
//once a match is found, statement R = V/I will be
printed
default: printf("Wrong selection.");
//once a match is found, statement Wrong
selection. will be printed
}
If user picks 'B', selection=‘B’, output is: 6-37
I = V/R R = V/I Wrong selection.
Program 6.6b (with ‘break’)
switch (selection)
{case 'A': printf("V = I*R");
break;
//selection=‘B’ does not match ‘A’ so printf()and
break are not executed
case 'B': printf("I = V/R");
break;
//selection=‘B’ matches ‘B’ so I = V/R is printed.
Since is break present, the remaining statements
below are ignored
case 'C': printf("R = V/I");
break;
default: printf("Wrong selection. ");
}
If user again picks 'B', selection=‘B’, output is: 6-38
I = V/R
The Conditional Operator (?:)
6-40