The if else statement
if(condition) statement;
else statement;
if(condition) {
statements;
} else {
statements;
}
if(condition) statement;
else statement;
if(condition) {
statements;
} else {
statements;
}
If else statement is a conditional statement. If statement evaluates an expression as a condition, and if the value of the expression is true, the statement or block with curly braces after if, will get executed. If condition is not true else statement will get executed, if there is any. But it's not necessary to have else statement. But, you can not have else statement without any if statement.
Here is the example -
Here is the example -
Class IfElse1 (IfElse1.java)
public class IfElse1 { public static void main(String args[]) { int i = 10; if(i > 10) System.out.println("Greater"); else System.out.println("Less"); } }In above example, 10 as value is assigned to an integer variable i. Then in if condition, it is checked whether I is greater than 10. It is false, so the else statement will be executed and then the result will be - "Less". But if you change the condition as I >= 10 =>
if(I >= 10) System.out.println("Greater and Equal");
else System.out.prinln("Less");
It will print first statement after if as it will evaluate condition as true. You can have any simple or complex expression in if condition but expression should return always a boolean value - true / false. There can be more than one statements to be executed for one condition, but you have to put all the statements in one block means enclosed in curly braces. Here is another example - In this example, user enters a number and program checks if the number is even or odd, and executes the block statements according to the result -
Class IfElse2 (IfElse2.java)
import java.util.Scanner; public class IfElse2 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int i = sc.nextInt(); int q = i / 2; if((i % 2) == 0) { System.out.println("Number is even"); System.out.println("Half of the value is " + q); } else { System.out.println("Number is odd"); System.out.println("Half of the value will be a double value"); } } }Nested Ifs
There can be another condition in if or else statement itself. And else statement always connected to only the nearest if condition if it is not explicitly specified in blocks / curly braces.
if(condition1)
if(condition2) statement;
else statement;
Here, we have put two if conditions which are perfectly fine. If first if condition1 is true, it will check another condition condition2 and if that is true, execute the statement after second if condition. If second condition fails, else part will get executed. But, here is important thing - if first condition fails, else statement will not get executed as else is always connected to the nearest if condition. So, else statement will get executed only when second condition fails. But if you want to execute else statement if first condition fails, you have to put curly braces like this -
if(condition1) {
if(condition2) statement;
}
else statement;
If-else-if ladder
You can have multiple conditions to evaluate and multiple if else for each condition. Expressions are evaluated from the top downward. As soon as the condition is found true, the statements for the if / else block will get executed. The if-else-if ladder will look like this -
if(condition1) statement;
else if(condition2) statement;
else if(condition3) statement;
else if(condition4) statement;
......
else statement;
Here is the code example - It evaluates entered value i and evaluates the condition from top. It checks if the number is in particular range. Prints the message if the number in specified range. else it checks another range. If the number is not less than or equal to 20, it prints last else statement.
Class IfElse3 (IfElse3.java)
import java.util.Scanner; public class IfElse3 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number"); int i = sc.nextInt(); if(i <= 2) System.out.println(i + " <= 2"); else if(i <= 5) System.out.println(i + " > 2 and " + i + " <= 5"); else if(i <= 10) System.out.println(i + " > 5 and " + i + " <= 10"); else if(i <= 20) System.out.println(i + " > 10 and " + i + " <= 20"); else System.out.println(i + " > 20"); } }
No comments:
Post a Comment