Logout
Home
Topic 4
Last
Next
Conditionals Explained via a Conditionals Program
ConditionsPractice.java
1
11 public class ConditionsPractice {
12
13 public static void main(String[] args) {
14 Scanner snr = new Scanner(System.in));
15
16 System.out.println("Welcome to our Conditionals Lesson");
17 System.out.println("");
18 System.out.println("The classic "if true"");
19 if (true) {
20 System.out.println("The condition (true) was true");
21 } else {
22 System.out.println("The condition (true) was false.");
23 }
24 System.out.println("");
25 System.out.println("-----------------------");
26
27
28 System.out.println("Type in a boolean value for variable b1: (true/false).");
29 boolean b1 = snr.nextBoolean( );
30
31 System.out.println("");
32
33 if (b1) {
34 System.out.println("The condition (b1) was true");
35 } else {
36 System.out.println("The condition (b1) was false.");
37 }
38
39 System.out.println("");
40 System.out.println("");
41 System.out.println("-----------------------");
42
43 System.out.println("Type in a boolean value for variable b2: (true/false).");
44 boolean b2 = snr.nextBoolean( );
45 System.out.println("Type in a boolean value for variable b3: (true/false).");
46 boolean b3 = snr.nextBoolean( );
47
48
49 System.out.println("");
50 if (b2 && b3) {// && means AND
51 System.out.println("Both b2 and b3 are true.");
52 }else{
53 System.out.println("At least one of b2 and b3 is false");
54 }
55
56 if (b2 || b2) {// || means OR
57 System.out.println("At least one of b2 and b3 are true.");
58 }else{
59 System.out.println("Neither b2 nor b3 are true");
60 }
61
62 if (b2 ^ b3) {// ^ means XOR one or the other but not both
63 System.out.println("Either b2 or b3 was true, but not both of them.");
64 }else{
65 System.out.println("b2 and b3 are the same; either both true or both false.");
66 }
73 }
74 }
75
76