Introduction to C + +: Logical Operators and Conditional Operators

Doorsteptutor material for UGC is prepared by world's top subject experts: Get complete video lectures from top expert with unlimited validity: cover entire syllabus, expected topics, in full detail- anytime and anywhere & ask your doubts to top experts.

Logical Operators

The logical operators are used to combine one or more relational expression. The following table shows the logical operators.

The NOT operator is called the unary operator because it requires only one operand.

Example 2

• The first expressionevaluates to false and second expression evaluates to true. Therefore, the final expression is false.

• In AND operation, if any one of the expressions is false, the entire expression is false

• In OR operation, if any one of the expressions is true, the entire expression is true.

• In NOT operation, only one expression is required.

• If the expression is true, the NOT operation of true is false and vice versa.

• Unary operators

• C++ provides two unary operators for which only one variable is required.

For Example, 3

• Here plus sign and minus sign are unary because they are not used between two variables.

• Assignment operator

• The assignment stores the value of the expression on the right-hand side of the equal sign to the operand on the left-hand side.

Example 4

In addition to standard assignment operator shown above, C++ also supports compound assignment operators.

Let us study the use of compound assignment operators in the following table:

Example 5

/ / first

/ / second

In the second statement, the value of x is 7

Conditional Operator

• The conditional operator is called ternary operator as it requires three operands. The format of the conditional operator is:

• Conditional expression? expression: expression 2.

• If the value of Conditional expression is true then the expression 1 is evaluated, otherwise expression 2 is evaluated.

Example 6

• The condition evaluates to false, therefore big gets the value from b and it becomes 6.

• The comma operator

• The comma operator gives left to right evaluation of expressions.

• It enables to put more than one expression separated by comma on a single line.

Developed by: