You are on page 1of 6

SELECTION-MAKING DECISIONS, REPETITION

Logical Data and Operators

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

Evaluating Logical Expressions

There are two ways to evaluate the binary logical relationships:


Complete Evaluation.
Short Circuit Evaluation.

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

C provides 6 comparative operators. Comparative operators are divided into two


categories:
Relational Operators.
Equality 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

C implements two-way selection with if.else statement. An if.else statement is a


composite statement used to make a decision between two alternatives.
Fig below shows the logic flow for an if.else statement.

If (expression)
Statement 1
Else
Statement 2

The most important points to be remembered about if.else statements.


1. The expression must be enclosed in parentheses
2. No semicolon is needed for an ifelse statement; statement 1 and statement 2
may have a semicolon as required by their types.
3. The expression can have a side effect.
4. Both the true and the false statement can be any statement (even another if.else
statement) or they can be a null statement.
5. Both statement 1 and statement 2 must be one and only one statement.
6. We can swap the position of statement 1 and statement 2 if we use the
complement of the original expression.

Null else Statement


Even though there are two possible actions after a decision. If we are interested only in true
action, the false action can be left out using NULL statement. The NULL statement used in else
part of ifelse statement is called as NULL else statement (a null statement consist of only a
semicolon).

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

Dangling else Problem

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.

You might also like