-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02. ConditionalStatements.java
More file actions
132 lines (115 loc) · 3.34 KB
/
02. ConditionalStatements.java
File metadata and controls
132 lines (115 loc) · 3.34 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* Conditional Statements
1. if, else
2. else if
3. switch
4. break
These are basically some Java Keywords */
//Adult Age Calculator
import java.util.*;
public class main {
public static void main(String[] args) {
System.out.print("Enter the age: ");
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
if(age>=18) {
System.out.println("Adult");
} else {
System.out.println("Not Adult");
}
}
}
//Even-Odd Number Calculator
import java.util.*;
public class main {
public static void main(String[] args) {
System.out.print("Enter the number: ");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if(num % 2 == 0) {
System.out.println("It's an even number.");
} else {
System.out.println("It's an odd number.");
}
}
}
//How to use if-else condition
import java.util.*;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of a: ");
int a = sc.nextInt();
System.out.print("Enter the value of b: ");
int b = sc.nextInt();
if(a == b) {
System.out.println("a and b are equal.");
}
else if(a > b) {
System.out.println("a is greater than b.");
}
else if(a < b) {
System.out.println("a is lesser than b.");
}
else {
System.out.println("NOT DEFINED!");
}
}
}
/*If there is only one single statement inside the if condition adding curly braces is fully optional. It's your choice, but if it has multiple statements then you must write them inside the curly braces.
if(condition)
statement1: xyz;
if(condition) {
statement1: xyz;
statement2: abc;
statement3: pqr;
} */
import java.util.*;
public class main {
public static void main(String[] args) {
//switch case
Scanner sc = new Scanner(System.in);
int button = sc.nextInt();
switch(button) {
case 1: System.out.println("Hello!");
break;
case 2: System.out.println("Namaste!");
break;
case 3: System.out.println("Bonjour!");
break;
default: System.out.println("Invalid Button!");
}
}
}
//Simple Calculator
import java.util.*;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of a: ");
int a = sc.nextInt();
System.out.print("Enter the value of b: ");
int b = sc.nextInt();
System.out.print("Enter your choice [1) +, 2) -, 3) *, 4) /, 5) %]: ");
int operator = sc.nextInt();
/*
1 -> + [a + b]
2 -> - [a - b]
3 -> * [a * b]
4 -> / [a / b]
5 -> % [a % b]
*/
switch(operator) {
case 1: System.out.println(a+b);
break;
case 2: System.out.println(a-b);
break;
case 3: System.out.println(a*b);
break;
case 4: System.out.println(a/b);
break;
case 5: System.out.println(a%b);
break;
default: System.out.println("INVALID");
}
}
}