Boolean Expressions

Download Report

Transcript Boolean Expressions

Conditional Expressions
Control Statements
A control statement will or will not be executed based on the value
of the conditional expression
scanf("%d", &pressureValue);
while (pressureValue <= MAX_PRESSURE)
{
if (pressureValue == NOMINAL_VALUE)
printf("Pressure is nominal\n");
else
printf("Pressure is %d pounds\n", pressureValue);
for (i = 5; i <= pressureValue; i = i + 10)
printf("+");
printf("\n");
scanf("%d", &pressureValue);
}
// End while
Boolean Type
• A conditional expression evaluates to a value of true or false
• These are the two values for the Boolean type named after the
mathematician George Boole
• When a conditional expression is evaluated, it results in a value
of type Boolean
• In the C programming language there is no explicit Boolean
type. Instead, false is 0 and true is any non-zero value. Many
C programs use a #define macro to set a Boolean variable to
true or false
#define FALSE
#define TRUE
0
1
• Conditional expressions are also called Boolean expressions
• A conditional expression is used whenever a true or false
decision needs to be made about the value of certain variables
or constants in a program
Expression Contents
• A conditional expression in C may contain
any valid mathematical expression
• It may also contain relational operators and
logical operators
• In addition, it may contain one or more
function calls that return a value
Relational Operators
==,!=
<, <=
>, >=
if (A ==
if (A !=
for (i =
if (A <=
while (A
if (A >=
A = B <=
D = A ==
Equivalent, Not equivalent
Less than, Less than or equals
Greater than, Greater than or equals
B) C
B) C
0; i
B) D
> B)
B) D
C;
B;
= 5;
= 10;
< MAX_INDEX; i++) printf("*");
= 20;
B++;
= 40;
Logical Operators
&&
||
!
Logical AND
Logical OR
Logical NOT
if ( (A == B) && (C <= D) ) E = 5;
if ( (A != B) || (C >= D) ) F = 10;
while ( !(A == B) ) B++;
if ( !((A >= B) && (C <= D) || (E != F)))
G = 100;
A = B || C;
D = A && B;
Boolean Algebra
•
•
•
•
•
•
•
•
NOT false = true
NOT true = false
true AND true = true
true AND false = false
true OR true = true
true OR false = true
false AND false = false
false OR false = false
if (0 || 0) printf("Does false OR false = true?");
if (0 && 0) printf("Does false AND false = true?");
More Examples
ch1 = 'a';
ch2 = 'a';
printf("ch1 OR ch2 = %d\n", ch1 || ch2);
printf("ch1 AND ch2 = %d\n", ch1 && ch2);
// 1
// 1
ch1 = 'd';
ch2 = 'f';
printf("ch1 OR ch2 = %d\n", ch1 || ch2);
printf("ch1 AND ch2 = %d\n", ch1 && ch2);
// 1
// 1
ch1 = 'a';
ch2 = '\0';
printf("ch1 OR ch2 = %d\n", ch1 || ch2);
printf("ch1 AND ch2 = %d\n", ch1 && ch2);
// 1
// 0
Example with Logical and Relational
Operators
while ( (w <= x) && ( y >= z) )
{
if ( (w < MAX_HEIGHT) || (y > MAX_DISTANCE) )
{
w = w + newHeight;
y = y - newDistance;
newValuesUsed = TRUE;
}
else
{
w = w + oldHeight;
y = y - oldDistance;
newValuesUsed = FALSE;
} // End if
if ( !silentMode ) // Use of negative logic
printf("Height: %d Distance: &d\n", w, y);
} // End while
Operator Precedence
• Precedence refers to the order in which operations are
evaluated in an expression
a = w * x + !y || -u – v && b % c / d++;
• There is a hierarchy of rules that tells which operators
are evaluated before other operators (i.e., unary
before binary operators, multiplication before
addition, relational operators before logical operators)
• The highest precedence goes to parentheses
Rule of thumb: Avoid complicated
expressions and use parentheses
Special Notes
• Avoid complicated conditional expressions, especially
– When they are not a part of the original procedural design
– When they are created just to get the program to finally run
• Use meaningful names for Boolean variables and
declare them of type int
– Ex. valueFound, errorStatus, moreData
• Do not compare Boolean variables to TRUE or FALSE
if (valueFound == TRUE) X = 5;
if (valueFound) X = 5;
// Bad practice
// Good practice
if (valueFound == FALSE) X = 5;
if (!valueFound) X = 5;
// Bad practice
// Good practice
Special Notes (continued)
• Beware of the bitwise operators; they are not the same
as the logical operators
&
|
bitwise AND
bitwise OR
if (4 & 2) printf("Is this true?");
// Bitwise
if (4 && 2) printf("Or is this true?"); // Logical
• Do not user == or != operator with values of type float
double v = 0.999999999999999999999999999;
. . .
if (v == ZERO) X = 10;
// Bad
if ((v >= -0.001) && (v <= 0.001)) x = 10; // Good
