Possible Duplicate:
Why do we usually use||
not|
, what is the difference?
Example:
if(a && b && d)
{
...
}
I need to find out if a language supports checking all conditions in the if statement even if "b" fails. What is this concept called?
Possible Duplicate:
Why do we usually use||
not|
, what is the difference?
Example:
if(a && b && d)
{
...
}
I need to find out if a language supports checking all conditions in the if statement even if "b" fails. What is this concept called?
No, neither Java nor C++ will evaluate d
. This is short-circuit evaluation.
No, the binary logical operators are short-circuited. They evaluate their operands from left to right. If one of the operands evaluates such that the expression will be false then no other operands are evaluated.
The standatd binary opeartions && and || are short-circuited. If you want to force both sides to evaluate, use & or | instead of && and ||. e.g.
public class StackOverflow {
static boolean false1() {
System.out.println("In false1");
return false;
}
static boolean false2() {
System.out.println("In false2");
return false;
}
public static void main(String[] args) {
System.out.println("shortcircuit");
boolean b = false1() && false2();
System.out.println("full evaluation");
b = false1() & false2();
}
}