Chapter 5
Boolean expressions and Branching
Boolean Algebra
Boolean algebra is a simple but important concept used in computer science.
In boolean algebra variable can only contain one of two possible values:
TRUE or FALSE. Boolean algebra has several operators called LOGICAL OPERATORS
They are: ==, <, >, <=, >=, !=, &&, ||, !
Unlike Pascal, ANSI C does not have a built in boolean data type. Instead,
it uses integer variables to perform boolean operations. FALSE is defined
as the number 0. TRUE is defined as the number -1. C provides the following
boolean operators: ==, <, >, <=, >=, !=, &&, ||, !
"==" Equals
The "equals" operator is used for comparing two variables.
Notice that this operator has two equals signs. It is very important that
you include two equals signs when using this operator. If only one is included
the compiler will think you are trying to make an assignment statement instead
of a logical statement.
| First |
Operation |
Second |
Result |
| 1 |
== |
1 |
TRUE |
| 5 |
== |
1 |
FALSE |
"<", ">", "<=" and ">="
Less then, Greater than, Less than or equal to, Greater than or equal to
The greater than and less than symbols are also used for comparing variables.
| First |
Operation |
Second |
Result |
| 1 |
< |
2 |
TRUE |
| 1 |
< |
0 |
FALSE |
| 1 |
< |
1 |
FALSE |
| 2 |
> |
1 |
TRUE |
| 2 |
> |
3 |
FALSE |
| 2 |
> |
2 |
FALSE |
| 1 |
<= |
2 |
TRUE |
| 1 |
<= |
0 |
FALSE |
| 1 |
<= |
1 |
TRUE |
| 2 |
>= |
1 |
TRUE |
| 2 |
>= |
3 |
FALSE |
| 3 |
>= |
3 |
TRUE |
"!=" Not Equal to
This returns True if and only if both variables are different.
| First |
Operation |
Second |
Result |
| 1 |
!= |
0 |
TRUE |
| 1 |
!= |
1 |
FALSE |
&& "And operator"
The && operator performs the same function as the AND operator
in Pascal. It returns TRUE if and only if both the second and the first
variables are true. If one of the two other variables happens to be FALSE,
the result will be FALSE.
| First |
Operation |
Second |
Result |
| TRUE |
&& |
TRUE |
TRUE |
| TRUE |
&& |
FALSE |
FALSE |
| FALSE |
&& |
FALSE |
FALSE |
| FALSE |
&& |
TRUE |
FALSE |
|| "OR operator"
The || performs the same funciton as the OR operator in Pascal. It returns
TRUE if either one of the two variables is TRUE. OR only returns FALSE if
both variables are FALSE.
| First |
Operator |
Second |
Result |
| TRUE |
|| |
TRUE |
TRUE |
| TRUE |
|| |
FALSE |
TRUE |
| FALSE |
|| |
FALSE |
FALSE |
| FALSE |
|| |
TRUE |
TRUE |
! NOT
The NOT operator only takes one variable. It returns the complete opposite
of the variable that follows it.
| Operator |
First |
Result |
| ! |
TRUE |
FALSE |
| ! |
FALSE |
TRUE |
THE IF STATEMENT
The format for the IF statement is: if( <Boolean Expression> )
{ <body> }
Example
if(10 > 1) {
puts("true"):
}
Example (with ELSE clause):
if(10 > 12) {
puts("true");
} else {
puts("false");
}
The SWITCH statement
The SWITCH statement can be used to compare a value with several other
values.
switch(x) {
case 1 : printf("its one");
break;
case 2 : printf("its two");
break;
case 3 : printf("its three");
break;
case default : printf("not a choice"):
}
|