forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLogicalOpers.java
More file actions
56 lines (45 loc) · 1.35 KB
/
LogicalOpers.java
File metadata and controls
56 lines (45 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class LogicalOpers {
public static void main(String[] args) {
int x = 0;
int y = 0;
// nested
if (x == 0) {
if (y == 0) {
System.out.println("Both x and y are zero");
}
}
// combined
if (x == 0 && y == 0) {
System.out.println("Both x and y are zero");
}
// chained
if (x == 0) {
System.out.println("Either x or y is zero");
} else if (y == 0) {
System.out.println("Either x or y is zero");
}
// combined
if (x == 0 || y == 0) {
System.out.println("Either x or y is zero");
}
// De Morgan's
if (!(x == 0 || y == 0)) {
System.out.println("Neither x nor y is zero");
}
if (x != 0 && y != 0) {
System.out.println("Neither x nor y is zero");
}
// boolean variables
boolean flag;
flag = true;
boolean testResult = false;
boolean evenFlag = (x % 2 == 0); // true if x is even
boolean positiveFlag = (x > 0); // true if x is positive
if (evenFlag) {
System.out.println("n was even when I checked it");
}
if (!evenFlag) {
System.out.println("n was odd when I checked it");
}
}
}