2015-09-07 11:44:36 -06:00
|
|
|
|
// operators/ShortCircuit.java
|
2015-06-15 17:47:35 -07:00
|
|
|
|
// <20>2015 MindView LLC: see Copyright.txt
|
|
|
|
|
// Demonstrates short-circuiting behavior
|
|
|
|
|
// with logical operators.
|
|
|
|
|
import static com.mindviewinc.util.Print.*;
|
|
|
|
|
|
|
|
|
|
public class ShortCircuit {
|
|
|
|
|
static boolean test1(int val) {
|
|
|
|
|
print("test1(" + val + ")");
|
|
|
|
|
print("result: " + (val < 1));
|
|
|
|
|
return val < 1;
|
|
|
|
|
}
|
|
|
|
|
static boolean test2(int val) {
|
|
|
|
|
print("test2(" + val + ")");
|
|
|
|
|
print("result: " + (val < 2));
|
|
|
|
|
return val < 2;
|
|
|
|
|
}
|
|
|
|
|
static boolean test3(int val) {
|
|
|
|
|
print("test3(" + val + ")");
|
|
|
|
|
print("result: " + (val < 3));
|
|
|
|
|
return val < 3;
|
|
|
|
|
}
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
boolean b = test1(0) && test2(2) && test3(2);
|
|
|
|
|
print("expression is " + b);
|
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
|
}
|
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
|
test1(0)
|
|
|
|
|
result: true
|
|
|
|
|
test2(2)
|
|
|
|
|
result: false
|
|
|
|
|
expression is false
|
2015-09-07 11:44:36 -06:00
|
|
|
|
*/
|