Index

chapter1

chapter2page1

chapter2page2

chapter2page3

chapter2page4

chapter2page5

chapter2page6

chapter2page7

chapter2page8

chapter2page9

chapter2page10

chapter2page11

chapter2page12

chapter2page13

chapter2page14

chapter2page15

chapter2page16

chapter2page17

chapter2page18

chapter2page19

chapter2page20

chapter2page21

chapter2page23

chapter2page24

chapter2page25

chapter2page26

chapter2page27

chapter2page28

chapter2page29

chapter2page30

chapter2page31

chapter2page32

chapter2page33

chapter2page34

chapter3

link one

link one

link one

link one

link one

link one

2.6.6 - Conditional Operators

Conditional Operators take boolean operands and return boolean results. There are three conditional operators, Conditional-AND &&, Conditional-OR || and the unique ternary operator of Java which takes three operands, the Conditional operator ? :

2.6.6.1 - Conditional-AND Operator &&

Conditional-AND operator && is similar to the logical AND operator &. The only difference between them is the Conditional-AND operator returns immediately false, when the value of the left operator is false and in that case, the right operand is not evaluated. In the conditional-AND operator the right operand is only evaluated when the value of the left operand result to false, whereas the right operand of the logical AND is aiways evaluated whatever the value of both operands. This is the only difference between conditional-AND and logical AND but this is an important difference, because in conditional-AND operator, there is a possibily of not evaluating the right operand and this gives to conditional-AND operator a chance to resolve its operands quicker than the logical AND. This possibiliy of quikness is the reason of preference of conditional-AND over the logical AND.

The application of conditional and logical AND's are exactly the same except the usage of only one & for logical AND and two && for conditional-AND. The similarity is so superposed that we can even use the same applications for both of the operators. The application below is the same application given in section 2.6.5.2.1 but with a && instead of the single & :

package preliminary;
public class ConditionalAND {
public static void main(String[] args){
boolean controlFalse = false;
boolean controlTrue = true;
System.out.print(" true & true = "  +  controlTrue & controlTrue);
System.out.print(" true & false = "  +  controlTrue & controlFalse);
System.out.print(" false & false = "  +  controlFalse & controlFalse);
 } // end main
} //end class ConditionalAnd

The result of this program :

        true & true = true
        true & false = false
        false & false = false

As it can be understood from the results of the above program, logical and conditional AND's returns true only if both of the operands are true. they will return false in all other cases. But the possibiliy of evaluating conditional expressions quicker make conditional-AND, the operator of choice over logical OR.

2.6.6.2 - Conditional OR Operator ||

The conditional-OR operator is exacly like logical OR operator, except the left side is evaluated first and if it is evaluated as true, the right side is never evaluated. This may enhance the speed of evaluation and that is why conditional-OR is preferred over logical OR.

Just like the logical OR, the conditional-OR operator returns true, only when one of his operands is true, it returns false otherwise. The example given below is exactly the same example given in section 2.6.5.2.2

package preliminary;
public class ConditionalOR {
public static void main(String[] args){
boolean controlFalse = false;
boolean controlTrue = true;
System.out.print(" true | true = "  +  (controlTrue | controlTrue));
System.out.print(" true | false = "  +  (controlTrue | controlFalse));
System.out.print(" false | false = "  +  (controlFalse | controlFalse));
 } // end main
} //end class ConditionalOR

The result of this program :

true | true = true
true | false = true
false | false = false

The result obtained is exactly the same with the logical OR, but conditional-OR is preferred in programs, because there is a possibility to by-pass the evalautuaion of the right operand and therefore gaining speed in the evalaution of the conditional expression.

2.6.6.3 - Ternary Conditional Operator ? :

Ternary conditional operator is the unique operator in Java (in JavaScript too) which takes three operands. The first operand must be boolean otherwise a compilation error is generated. The remaining second and third operands are expressions. The placement of the operands in ternary conditional operator is as follows:

First Operand (Boolean) ? Second Operand (Expression) :Third Operand (Expression)

If the first operand is true than second operand is executed otherwise the third operand is executed.

The ternary conditional operator is applied as an alternative to using if statements for changing the program execution sequence i.e. program branching. We will introduce the application examples of the ternary operator, when together with the program branching using conditional expressions.

<< Index

Polyglott HTML5(XHTML5 compliant HTML5 code)