File 20
File 20
– Repetition [Chapter 5]
A B (A && B) (A || B) !A
true true true true false
true false false true false
false true false true true
false false false false true
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?
x is equal to 1 or 3 x == 1 || x == 3
Example:
if (x != 0.0)
product = product * x ;
Example:
if (x >= 0.0) printf("Positive");
else printf("Negative");
ch = ch – 'A' + 'a';
else
}
CSE 115 Programming Language I ECE@NSU
Hand Tracing an if Statement
if (x > y) /* switch x and y */
{
temp = x; /* save x in temp */
x = y; /* x becomes y */
y = temp; /* y becomes old x */
}
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;
• switch statement
– Syntax is more readable
– Implemented more efficiently in machine language
– Use switch whenever there are few case labels
– Use default for values outside the set of case labels
!(x == 1 || x == 3) x != 1 && x != 3