Decision Making Statements
Decision-making statements are one of the control flow statements which decide which statement to execute and when. It evaluate the boolean condition and control the execution of the block still the boolean condition provided is true.There are two types of decision-making statements in Java
1) if statement
2) switch statement
1) if Statement:
The if statement evaluates a condition and executes a block of code if the condition is true. In Java, there are four types of if-statements.
1) if statement
2) if-else statement
3) if-else-if ladder
4) nested-if statement
1) if
if statement the most basic statement among all control flow statements in Java. if statement is to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statements is executed otherwise not.
if syntax :
if(condition)
{
// Execution of block is condition is true
}
Program : Java if statement Program
public class ifProgram {
public static void main(String[] args) {
int x = 90;
if(x==90) {
System.out.println("X is equal to 90");
}
}
}
Output :
X is equal to 90
Flowchart if
2) if-else
if-else statement is an extension to the if-statement, in which if the if-block is evaluated as false then the else block gets executed
if-else syntax :
if(condition)
{
// Execution of if-block if condition is true
}
else{
// Execution of else-block if condition is false
}
Program : Java if-else Program
public class ifElseProgram {
public static void main(String args[])
{
int x = 90;
if (x < 30)
System.out.println("x is smaller than 30");
else
System.out.println("x is greater than 30");
}
}
Output :
x is greater than 30
Flowchart if-else
3) if-else-if ladder
if-else-if is a chain of if-else statements that create a decision tree in which the block of code get executed only if the boolean condition evaluated is true otherwise the else block gets executed.
if-else-if ladder syntax
if(condition 1) {
// Execution of if-block if condition 1 is true
}
else if(condition 2) {
// Execution of else-if-block if condition 2 is true
}
else {
// Execution of else-block if condition 1 and 2 are false
}
Program : Java if-else-if ladder Program
public class ifElseIfLadderProgram {
public static void main(String args[])
{
int x = 30;
if (x == 12)
System.out.println("x is 12");
else if (x == 17)
System.out.println("x is 17");
else if (x == 30)
System.out.println("x is 30");
else
System.out.println("x is not present");
}
}
Output :
x is 30
Flowchart if-else-if ladder
4) nested-if
Nested if statements simply mean an if statement inside an if statement. Java allows to use nest if statements within if statements where we can place an if statement inside another if statement. In nested if-statements, the if statement can contain a if or if-else statement inside another if or else-if statement.
nested-if syntax
if(condition 1) {
statement 1; // Execution of statement 1 if condition 1 is true
if(condition 2) {
statement 2; // Execution of statement 2 if condition 2 is true
}
else{
// Execution of else-block if condition 2 is false
}
}
Program : Java nested-if Program
public class nestedIfProgram {
public static void main(String args[])
{
int x = 24;
if (x > 21) {
if (x > 25)
System.out.println("x is greater than 25");
if (x > 20)
System.out.println("x is greater than 20");
} else{
System.out.println("x is smaller than 21");
}
}
}
Output :
x is greater than 20
Flowchart nested-if
switch-case
In Java, Switch statements are similar to if-else-if statement.The switch statement is a multiway branch statement that contains multiple blocks of code called cases and a single case is executed based on the variable which is being switched.
switch-case syntax
switch (expression)
{
case value1:
statement1; // Execution of statement 1 if case is value 1
break;
case value2:
statement2; // Execution of statement 2 if case is value 2
break;
.
.
case valueN:
statementN; // Execution of statement N if case is value N
break;
default:
dfaultStatement; // Execution of default statement if none of above value gets executed
}
Program : Java switch-case Program
public class switchCaseProgram {
public static void main (String[] args) {
int num=25;
switch(num){
case 10 : System.out.println("Number is 10");
break;
case 15 : System.out.println("Number is 15");
break;
case 20 : System.out.println("Number is 20");
break;
case 25 : System.out.println("Number is 25");
break;
default: System.out.println("Number is Not present");
}
}
}
Output :
Number is 25
Flowchart switch-case