Академический Документы
Профессиональный Документы
Культура Документы
Class 10
Selection –
Making Decisions
Relational, Equality, and Logical
Operators
Equality equal to ==
Operators not equal to !=
positive 0 1 0 1
zero 0 0 1 1
negative 1 0 1 0
Equivalent
Expression Expression Value
i == j j == i 0
i != j j != i 1
a !a
zero 1
nonzero 0
Evaluating Expressions
with Logical Negation
Declarations and Initializations
int i = 7, j = 7;
double x = 0.0, y = 999.9;
! i - j + 1 ((!i) - j) + 1 -6
! ! (x + 3.3) ! (! (x + 3.3)) 1
! x * ! ! y (! x) * (!(!y)) 1
Values of Logical Expressions
a b a && b a || b
zero zero 0 0
zero nonzero 0 1
nonzero nonzero 1 1
Truth Table for && and ||
a b a && b a || b
F F F F
F T F T
T F F T
T T T T
Precedence and Associativity of
&& and ||
Theprecedence of && is
higher than ||, but both
operators are of lower
precedence than all unary,
arithmetic, and relational
operators.
Evaluating Expressions with
Logical && and || Operators
Declarations and Initializations
int i = 3, j = 3, k = 3;
double x = 0.0, y = 2.3;
(i = 0) && (j = 3);
printf(“%d %d\n”, i, j); /* 0 2 */
i = 0 || (j = 4);
printf(“%d %d\n”, i, j); /* 1 4 */
(i = 2) || (j = 5);
printf(“%d %d\n”, i, j); /* 2 4 */
Example of the Use of
Short-Circuit Evaluation
if (a == 1)
if (b == 2)
printf(“***\n”);
The “Dangling else”
Semantic Problem
if (grade >= 80)
if (grade < 90)
printf(“Grade = B.\n”);
else /* Wrong */
printf(“Grade = A.\n”); /*Indenting*/
=========================================
if (grade >= 80)
if (grade < 90)
printf(“Grade = B.\n”);
else /* Correct */
printf(“Grade = A.\n”); /*Indenting*/
Solution to the “Dangling else”
An else attaches to the
nearest if without an else.
That’s why
if (grade >= 80)
if (grade < 90)
printf(“Grade = B.\n”);
else
printf(“Grade = A.\n”);
is the correct formatting.
/* Find the minimum of three values. */
#include <stdio.h>
int main(void)
{
int x, y, z, min;
printf(“Input three integers: “);
scanf(“%d%d%d”, &x, &y, &z);
if (x < y)
min = x;
else
min = y;
if (z < min)
min = z;
printf(“The minimum value is %d\n”, min);
return 0;
}
Nested Flow of Control
Flow-of-control statements
such as if can be nested.
A common nested flow-of-
control construct makes
repeated use of if-else
statements.
Nested if-else Statements
Could be Written Like This:
if (expr1) Except for next statement
statement1 this is a single if-else statement.
else The else with the default statement
if (expr2) is optional. The problem here is
statement2 that the nested else-if statements
else
march off the right side of the
if (expr3)
statement3 screen (or paper).
else
if (expr4)
statement4
else
default statement
next statement
Alternate Style
for else-if Statements
if (expr1) This is the style you will
statement1 see used and it is what
else if (expr2)
you should use.
statement2
else if (expr3)
statement3
else if (expr4)
statement4
else
default statement
next statement
A Ternary Operator
Theoperators you have
studied so far have been
either unary or binary
operators.
Unary Examples Binary Examples
-x a + b
!y j && k
Theconditional operator is a
ternary operator.
The Conditional Operator
General Form
expr1 ? expr2 : expr3
• First, expr1 is evaluated. If expr1
is nonzero (true), then expr2 is
evaluated, and that is the value of
the conditional expression as a
whole.
• If expr1 is zero (false), then expr3
is evaluated, and that is the value
of the conditional expression as a
whole.
Equivalent Code Using if-else
or a Conditional Expression
if (y < z)
x = y;
else
x = z;
x = (y < z) ? y : z;
37
The Switch Statement
• Each case
contains a value switch ( expression ){
and a list of case value1 :
statements statement-list1
case value2 :
• The flow of control statement-list2
case value3 :
transfers to statement-list3
statement case ...
associated with
the first case value }
that matches
38
Switch - syntax
• The general syntax of a switch statement is:
switch
switch ( expression ){
and
case value1 :
case
statement-list1
are
case value2 :
reserved
statement-list2
words
case value3 :
statement-list3
case ...
If expression
} matches value3,
control jumps
to here
The Switch Statement
• The break statement can
switch ( expression ){
be used as the last
case value1 :
statement in each case's statement-list1
statement list break;
case value2 :
• A break statement statement-list2
causes control to transfer break;
to the end of the switch case value3 :
statement statement-list3
break;
• If a break statement is case ...
not used, the flow of
control will continue into }
the next case
Switch Example
• Examples of the switch statement:
switch (option){
case 'A':
aCount++;
break;
case 'B':
bCount++;
break;
case 'C':
cCount++;
break;
}
Switch – no breaks!!!
• Another Example:
switch (option){
case 'A': switch (option){
aCount++; case 'A':
break; aCount++;
case 'B': case 'B':
bCount++; bCount++;
break; case 'C':
case 'C': cCount++;
cCount++; }
break;
}
Switch - default
• A switch statement can have an optional
default case
• The default case has no associated value and
simply uses the reserved word default