if-statement in java
If statement is an decision making statement.It will be executed if the condition is true.
Example
public class Main { public static void main(String[] args) { int a = 10; if(a>5) { System.out.println("a is greater"); } } }
Output
a is greater
if-else
If else statement is an decision making statement.It will be executed if the condition is true otherwise the else statement will be executed.
Example
public class Main { public static void main(String[] args) { int age = 19; if(age>18) { System.out.println("allow the party"); } else { System.out.println("not allow"); } } }
Output
allow the party
else-if
else-if statement is an decision making statement. else-if statement has two or more block of statement.which block of statement is true It will be executed. if all block of statement is false else block will be executed.
Example
public class Main { public static void main(String[] args) { int a = 3; if(a==1) { System.out.println("The number is one"); } else if(a==2) { System.out.println("The number is two"); } else if(a==3) { System.out.println("The number is three"); } else { System.out.println("greater than three"); } } }
Output
The number is three
nested-if else
Nested if else statement is an decision making statement. The if statement have a inside the if-else statement that is call nested-if else statement.
Example
public class Main { public static void main(String[] args) { int a = 5; if(a>0) { if(a%2==0) { System.out.println("even"); } else { System.out.println("odd"); } } else { System.out.println("negative"); } } }
Output
odd