Logical Expressions
Used with selection statements
Operator Type | Associativity | Precedence |
---|---|---|
Relational (<, >, ⇐, >=) | Left | Lower than Arithmetic |
Equality (!=, ==) | Left | Lower than Relational |
Logical (!) | Right | Same as Unary + and - |
Logical (&&, ||) | Left | Lower than Equality |
Conditional (Tertiary) | Left | Lower than Logical |
All the above operators have higher precedence than assignment operators
Non-zero Operands are True everything else is False
Logical operators can be short-circuited
Irrespective of the precedence of the operators logical expressions are evaluated condition by condition from left
Boolean Datatype
C89 does not have Boolean datatype
C99 added _Bool
which is an unsigned int in disguise which can take the values 0 and 1
If any other non-zero value is assigned to _Bool
it is saved as 1
The header <stdbool.h>
was also added which includes the type bool
which is the same as _Bool
. The header also defines macros that allow us to use true
and false
as values.
If Statement
Cascaded If Statement
Cascaded if statements aren’t a special kind of statement
They are ordinary if statements that happens to have another if statement as its else clause (and that if statement has another if statement as its else clause, as so on)
Dangling else Statement
In the above program the else
statement belong to the inner if
statement
C associates else
statements with the nearest if
statement that does not have a else
In the above example else
belongs to the outer if
Switch Statement
When there are many cases switch
statement will be faster than cascaded if statement
The expression supplied to the switch statement should be an integer value
Char can also be used since its treated as int by C but floats and strings cannot be used
The expression passed to the case statement must be a constant int (or char)
Variables cannot be used (unless its an macro) since they are not a constant value
Duplicate case labels are not allowed
The order of the case statements does not matter (default
does not have to be last)
Switch statement is a type of computed jump
Case statements are markers that represent different positions in the switch statement
When the last statement in a case is executed the control “falls through” to the first statement in the next case, the case label is ignored