Professional Documents
Culture Documents
A piece of data is called logical if it conveys the idea of true or false. Logical data are
created in answer to a question that needs a yes-no answer.
If a data value is zero, it is considered false, if it is non-zero it is considered true.
C has three logical operators
Not Operator
And Operator
OR operator
NOT Operator
The NOT operator (!) is a unary operator with precedence value 15 changes a true value
to false and false value to true.
X
FALSE
TRUE
!X
TRUE
FALSE
AND Operator
The AND operator (&&) is a binary operator with precedence value 5. The result is true
only when both the operands are true; its false in all other cases.
X
FALSE
FALSE
TRUE
TRUE
Y
FALSE
TRUE
FALSE
TRUE
X&&Y
FALSE
FALSE
FALSE
TRUE
OR Operator
The OR operator (| |) is a binary operator with precedence value 4. The result is false if both
operands are false; its true in all other case.
X
FALSE
FALSE
TRUE
TRUE
Y
FALSE
TRUE
FALSE
TRUE
X||Y
FALSE
TRUE
TRUE
TRUE
Complete Evaluation
In this method the expression must be completely evaluated before the result is determined. This
means that the AND expression must be completely evaluated even when the first operand is
false and its therefore known the result must be false.
Short Circuit Evaluation
In this method it sets the resulting value as soon as it is known. It does not need to
complete the evaluation. In the other words, it operates in a short circuit fashion and stops
the evaluation when it knows for sure what the final result will be.
Under this method, if the first operand of a logical AND expression is false, the second
half of the expression is not evaluated because it is apparent that result will be always
false.
TRUE
Comparative Operators
Comparative Operators are binary operators that accept two operands and compare them.
The result is Boolean type i.e., the result is always true or false.
The relational operators have a higher priority (10) in the precedence table.
The equality operators have precedence (9).
Type
Relational
Equality
Operator
<
<=
>
>=
==
!=
Meaning
Less than
Less than or equal
Greater than
Greater than or equal
Equal
Not Equal
Precedence
10
9
Two-Way Selection
The basic decision statement in the computer is done through the two-way selection.
Two-way selection is described to the computer as a conditional statement that can be
answered either true or false. If the answer is true, one or more action statements are
executed. If the answer is false then a different action or set of actions is executed.
The flowchart for two way decision logic is as shown below
If.else
If (expression)
Statement 1
Else
Statement 2
If (expression)
{
..
}
Else
; /* do
nothing*/
Nested if Statement
In ifelse the statement may be any statement including another if.else. When an
if.else is included within an ifelse such a statement is known as a nested if
statement.
Fig shows the nested if statement.
There is no limit to how many levels can be nested, but if there are more than three they
can become difficult to read
When there is no matching else for an if statement, it is called dangling else problem.
This problem is solved in C language always by pairing else with most recent unpaired if
in the block.