CSADPRG_mod4_short_circuit_evaluation

original file
this is unedited

short circuit evaluation

(a >= 0) && (b < 10)

Let's look at  a potential problem with non-short-circuit evaluation. Suppose Java did not use short-circuit evaluation, consider the following code for a table lookup. list which has listlen elements is  the array to be searched and key is the value we are looking for.

index = 0;  
while ((index < listlen) && (list[index] != key))  
    index = index + 1;

On the other hand, short-circuit evaluation exposes the potential problem of side-effects in expressions. Consider this Java expression
(a > b) || ((b++) / 3)

In the C-based languages, l AND and OR operators, && and ||, are short-circuit. However, these languages also have bitwise AND and OR operators, & and |, respectively, that can be used on Boolean-valued operands and are not short-circuit.