if else statements are very basic blocks to check conditions, according to condition(boolean expression) execute appropriate block.
if (booleanExpression) { // if booleanExpression is True this code will execute your code here.... } else { // if booleanExpression is False this code will execute your code here.... }
In below example, if block will be executed if x is greater than 11. Otherwise, the else block will be executed.
public class If_Else_Block { public static void main(String[] args) { int x = 10; if (x > 6) { System.out.println("x > 4 is true, if block execute. "); } else{ System.out.println("x > 4 is false then else block execute. "); } } }
x > 4 is true, if block execute.
In below example, if block will be executed if x is greater than 11. Otherwise, the else block will be executed.
public class If_Else_Block { public static void main(String[] args) { int x = 9; if (x > 11) { System.out.println("x > 11 is true, if block will execute. "); } else{ System.out.println("x > 11 is false then else block will execute. "); } } }
x > 11 is false so else block will execute.
If there is only one line code inside an if or else block, the curly braces ({) are optional.
public class If_Else_Block { public static void main(String[] args) { int x = 9; if (x > 11) System.out.println("x > 11 is true, if block will execute. "); else System.out.println("x > 11 is false then else block will execute. "); } }
x > 11 is false so else block will execute.
if there are many condition check then we can use ifelse block.
if (booleanExp_1) { // booleanExp_1 is True then this block code will execute. } else if (booleanExp_2) { // booleanExp_2 is True then this block code will execute. } ... else { // if all upper conditions will fail(False) then this block code will execute. }
If there are many conditions in elseif then which condition is true that block code will execute. .
public class If_Else_Block { public static void main(String[] args) { int c = 3; if (c == 1) { // False System.out.println("one"); } else if (c == 2) { // False System.out.println("two"); } else if (c == 3) { // True // This Block code will execute System.out.println("three"); } else { System.out.println("invalid"); } } }
three
If there are many conditions then, if all conditions are false then always else block code will execute.
public class If_Else_Block { public static void main(String[] args) { int c = 5; if (c == 1) { // False System.out.println("one"); } else if (c == 2) { // False System.out.println("two"); } else if (c == 3) { // False System.out.println("three"); } else { // This Block code will execute System.out.println("All expressions are false, else block executed."); } } }
All expressions are false, else block executed.
See how to use && and || operators for if else expressions.
For && both conditions should be True then it will True.
For || any one conditions is True then it will True.
Taking example for use of && operator.
public class If_Else_Block { public static void main(String[] args) { int c = 5; if (c == 1 && c == 5) { // False System.out.println("if block (c == 1 && c == 5) is executed"); } else if (c == 5 && c < 10) { // True System.out.println("elseif block (c == 5 && c < 10) is executed"); } else { // This upper both expressions are False then this block code execute. System.out.println("else block is executed"); } } }
elseif block (c == 5 && c < 10) is executed
Taking example for use of || operator.
public class If_Else_Block { public static void main(String[] args) { int c = 5; if (c == 1 || c == 5) { // True System.out.println("if block (c == 1 || c == 5) is executed"); } else if (c == 6 || c < 10) { // False System.out.println("elseif block (c == 6 || c < 10) is executed"); } else { // This upper both expressions are False then this block code execute. System.out.println("else block is executed"); } } }
if block (c == 1 || c == 5) is executed
Internallly Switch statement working is same as elseif blocks.
switch accept condition and contain many case statements, Whenever case statement is true that
case condition block code will execute, case contains break, when break will execute
switch will break and execution will out from switch statement.
switch (condition) { case value_1 : // You Code Here.... break; case value_2 : // You Code Here.... break; . . . case value_n : // You Code Here.... break; default: // You Code Here.... }
In below example, if block will be executed if x is greater than 11.
Otherwise, the else block will be executed.
public class Switch_Statement { public static void main(String[] args) { int a = 1; switch (a) { case 1 : System.out.println("First case."); break; case 2 : System.out.println("Second case."); break; case 3 : System.out.println("Third case."); break; default: // if none of the case System.out.println("Default."); } // Switch close } // Main close }
First case.
if we will not take break in case statement then switch will executed for other case until not found break or default statement.
public class Switch_Statement { public static void main(String[] args) { int a = 2; switch (a) { case 1 : System.out.println("First case."); break; case 2 : System.out.println("Second case."); case 3 : System.out.println("Third case."); default: // if none of the case System.out.println("Default."); } // Switch close } // Main close }
Second case. Third case. Default.
We can use nested Switch statements.
public class Switch_Statement { public static void main(String[] args) { int a = 1; int b = 2; switch (a) { case 1 : System.out.println("Outer First case."); // When case 1 is true then inner switch will call // Inner switch statement start switch (b) { case 1 : System.out.println("Inner First case."); break; case 2 : System.out.println("Inner Second case."); break; default: // if none of the case System.out.println("Inner Default."); } // Switch close // Inner switch statement end break; case 2 : System.out.println("Outer Second case."); break; default: // if none of the case System.out.println("Outer Default."); } // Switch close } // Main close }
Outer First case. Inner Second case.