1(condition) ? (what happens if true) : (what happens if false);
2
3boolean party = friday ? play("what is love") : play("The Sound of Silence");
1System.out.println("How many sattelites does JUPITER have ??");
2 C = scan.nextInt();
3
4 if (C == 79)
5 {
6 System.out.println("Congrats ! You got the Third Question Right :)");
7 points = points + 1;
8 }
9
10 else if (C != 79)
11 {
12 System.out.println("Sorry but your answer is wrong :(");
13 }
1else if statement is used to specify new condition if first condition is false.
2Syntax:
3
4if(condition1)
5{
6 // execute if condition1 is true
7}
8else if (condition2)
9{
10 // execute if condition2 is true
11}
12else if (condition3)
13{
14 // execute if condition3 is true
15}
16else
17{
18 // execute if conditions 1, 2 and 3 becomes false
19}
1import java.io.*;
2public class JavaIfElse
3{
4 public static void main(String[] args)
5 {
6 int number = 15;
7 // check if number is divisible by 2
8 if(number%2 == 0)
9 {
10 System.out.println(number + " is even number");
11 }
12 else
13 {
14 System.out.println(number + " is odd number");
15 }
16 }
17}
1if(a<b){
2 //if a<b you are here
3 //do something
4}else if(a==b){
5 //if a==b you are here
6 //do something
7}else{
8 //if none above conditions are matched you are here
9 //do something
10}
1int x = 3;
2
3if (x == 3) {
4 // block of code to be executed if the condition is true
5}