forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditional.java
More file actions
54 lines (44 loc) · 1.28 KB
/
Conditional.java
File metadata and controls
54 lines (44 loc) · 1.28 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
/**
* Examples from Chapter 5.
*/
public class Conditional {
public static void main(String[] args) {
String fruit1 = "Apple";
String fruit2 = "Orange";
System.out.println(fruit1.equals(fruit2));
int x = 17;
int n = 18;
if (x > 0) {
System.out.println("x is positive");
}
if (x % 2 == 0) {
System.out.println("x is even");
} else {
System.out.println("x is odd");
}
if (x > 0) {
System.out.println("x is positive");
} else if (x < 0) {
System.out.println("x is negative");
} else {
System.out.println("x is zero");
}
if (x == 0) {
System.out.println("x is zero");
} else {
if (x > 0) {
System.out.println("x is positive");
} else {
System.out.println("x is negative");
}
}
boolean evenFlag = (n % 2 == 0); // true if n 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");
}
}
}