Decision Making statements will have one or more conditions to validate and check whether conditions are satisified. In case the conditions satisfies Boolean value as “True”, a set of statements are executed within the block and a set of remaining statements will be executed if the condition satisifies as “false”
The Decision Making statements in Java are as below :
If Statement
Switch Statement
If Statement in Java : The if statement is a control flow based statement. Where in if statement will test the condition and executes the if block once the value returns as true.
Syntax :
1 2 3 4 5 6 7 |
if(condition) { //if condition is true this block is executed } |
Example on IF statement:
1 2 3 4 5 6 7 8 9 10 11 12 |
public class ifStatement_Example { public static void main(String[] args) { int val = 4; if(val==4) { System.out.println("condition is true"); } } } |
OUTPUT :
Java If statement output
In the above example we have seen how the if statement condition checks if the value is equal to 4 and since it is true, the if block is executed.
if else statement in Java :if else statements are those which validates the test condition. If the condition is true then if block is executed otherwise else block is executed.
Syntax of if else:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
if(condition) { //if condition is true this block is executed } else { //if condition is false this block is executed } |
Example on if else Statement :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class ifElseStatement_Example { public static void main(String[] args) { int val = 5; if(val==4) { System.out.println("condition is true"); } else{ System.out.println(“condition is false”); } } } |
OUTPUT :
Java If Else statement
In the above example we have seen how the if statement condition checks if the value is equal to 4 and since it is false, the else block is executed.
If -else-if statement in Java :
syntax of If -else-if statement in Java :
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 |
if(condition1) { //if condition1 is true this block is executed } else if(condition2) { //if condition is false this block is executed } … …… else { //common statement to be executed if all the conditions fails } |
In the above example we have seen how the if statement condition checks if the value is equal to 4, else another if condition checks whether value is equal to 6. since both the conditions fails, the else block is executed.
Switch Statement in Java : Switch statement executes single statement from multiple conditions based on our choice.
Syntax of Switch statement :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
switch(expression){ case value1: // value 1 code to be executed; break; //optional case value2: //value2 code to be executed; break; //optional …… default: Default code to be executed if none of the cases are not matched; } |
Example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class switchExample { public static void main(String[] args) { int value=20; switch(value){ case 10: System.out.println(“Case 10 execution”); break; case 20: System.out.println(“Case 20 execution”); break; case 30: System.out.println(“Case 30 execution”); break; default:System.out.println(“Value is not matched with 10, 20 or 30”); } } } |