Java Decision making statements
1) if Statement
2) if…else Statement
3) if…else…if…else Statement
4) Nested if statement
5) switch statement
if Statement:
- if statement consists of a Boolean expression followed by one or more statements.
- If the Boolean expression evaluates to true, then the block of code inside the if statement will be executed. If not, control jumps to next line outside if loop.
- Using if decision making Statement we verify condition is True or False to Execute code block.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package package_Java; public class If_Java { public static void main(String args[]) { int x = 10; if (x == 10) { System.out.print("This is if statement"); } } } |
Output:
1 | This is if statement |
if… else Statement:
- if statement can be followed by an optional else statement, which executes when the condition mentioned is false.
- Using if else decision making statement definitely execute any code block depends on condition.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package package_Java; public class If_Else_Java { public static void main(String args[]) { int x = 30; if (x == 20) { System.out.print("This is if statement"); } else { System.out.print("This is else statement"); } } } |
Output:
1 | This is else statement |
if… else…if… else Statement:
- if statement can be followed by an optional else if…else statement, which is very useful to test various conditions using single if…else if statement.
- Use to verify large conditions with multiple else block’s.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package package_Java; public class If_Else_If_Java { public static void main(String args[]) { int x = 30; if (x == 10) { System.out.print("Value of X is 10"); } else if (x == 20) { System.out.print("Value of X is 20"); } else if (x == 30) { System.out.print("Value of X is 30"); } else { System.out.print("This is else statement"); } } } |
Output:
1 | Value of X is 30 |
Nested if statement:
- When there is an if statement inside another if statement then it is called the nested if decision making statement.
- Use to verify multi condition’s at once.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class If_Nested_Java { public static void main(String args[]) { int num = 70; int i = 100; if (num == 70) { System.out.println("number is 70"); if (i == 100) { System.out.println("number is 100"); } } } } |
Output:
1 2 | number is 70 number is 100 |
switch statement:
- A switch decision making statement allows a variable to be tested against a list of values.
- Each value is called a case and the variable is being checked for each case.The body of a switch statement is referred to as a switch block.
- A statement in the switch block can be categorized with one or greater case or default labels.
- The switch statement evaluates its expression, then executes all statements that follow the matching case label.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package package_Java; public class Switch_Java { public static void main(String args[]) { char grade = 'D'; switch (grade) { case 'A': System.out.println("Excellent!"); break; case 'B': case 'C': System.out.println("Well done"); break; case 'D': System.out.println("You passed"); case 'F': System.out.println("Better try again"); break; default: System.out.println("Invalid grade"); } } } |
Output:
1 | You passed |