NOTE
- a == b # Equals
- a != b # Not Equals
- a > b # Greater than
- a < b # Less than
- a >= b # Greater than or equal to
- a <= b # Less than or equal to
If - Else if - Else Usage
1int x = 10
2if (x > 5) {
3 System.out.println("x is greater than 5");
4} else if (x > 3 ) {
5 System.out.println("x is greater than 3");
6} else {
7 System.out.println("x is less than 3");
8}
If Else Shorthand Usage
TIP
variable = (condition) ? expressionTrue : expressionFalse;
1// use this
2int x = 20;
3String result = (x > 10) ? "x is greater than 10" : "x is less than 10";
4System.out.println(result);
5
6// instead of using this
7int x = 10
8if (x > 5) {
9 System.out.println("x is greater than 5");
10} else {
11 System.out.println("x is less than 5");
12}
Nested If
1int x = 10
2if (x > 3) {
3 System.out.println("x is greater than 3");
4 if (x > 6) {
5 System.out.println("x is also greater than 6");
6 }
7 if (x > 8) {
8 System.out.println("x is also greater than 8");
9 }
10} else if (x > 2) {
11 System.out.println("x is 3");
12} else {
13 System.out.println("x is less than 2");
14}