-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEx5Condition.java
More file actions
75 lines (69 loc) · 2.23 KB
/
Ex5Condition.java
File metadata and controls
75 lines (69 loc) · 2.23 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package JavaEntry;
public class Ex5Condition {
public static void main(String[] arg){
// Check Simple condition
short x = 10;
short y = 20;
if(x > y){
System.out.println("X is greater than Y");
}else {
System.out.println("Y is greater than X");
}
// Check Multiple condition
short currentTime = 14;
if(currentTime < 10){
System.out.println("Good Morning");
}else if(currentTime > 10 && currentTime < 16){
System.out.println("Good Afternnon");
}else if(currentTime > 20){
System.out.println("Good Night");
}
// Shorthand condition
short age = 20;
String isAdult = age >= 18 ? "Adult" : "Not Adult";
System.out.println(isAdult);
// Instead of writing many if..else statements, we can use the switch statement.
short currentMonth = 5;
switch (currentMonth) {
case 1:
System.out.println("Month is January");
break;
case 2:
System.out.println("Month is February");
break;
case 3:
System.out.println("Month is March");
break;
case 4:
System.out.println("Month is April");
break;
case 5:
System.out.println("Month is May");
break;
case 6:
System.out.println("Month is June");
break;
case 7:
System.out.println("Month is July");
break;
case 8:
System.out.println("Month is August");
break;
case 9:
System.out.println("Month is September");
break;
case 10:
System.out.println("Month is October");
break;
case 11:
System.out.println("Month is November");
break;
case 12:
System.out.println("Month is December");
break;
default:
System.out.println("Number is not a valid month");
break;
}
}
}