Decision making in Java include controlling the flow of program execution based on some conditions. A programming language uses control statements to control the flow of execution of program based on certain conditions. Java provides several control statements to manage program flow properly which include:
- Conditional Statements: if , else-if , nested if , if-else-if
- Switch-Case: Switch statement in java control the flow of program by executing different blocks of code based on the value of a single variable or expression .
- Jump Statements : break, continue , return
Types Of Decision Making Statements:
- if
- if-else
- nested-if
- if-else-if
- switch-case
- jump, break , continue , return
Java if Statement:
Java if statement is a simple decision making statement which is used to decide whether a certain statement or block of statements will be executed or not.
Syntax:
if ( condition ) {
//Statements to be executed if the condition is true
}
NOTE:
- if is in lowercase letter , uppercase letter (If or IF) will generate an error.
- if statement must result in boolean values.
- If we don’t use curly braces ( { } ) in if statement , only the next line after the if is considered as part of the if block .
For Example :
boolean isEligible = true;
if ( isEligible )
//Statement1
System.out.println(” Eligible for Gifts” ) ; // Belongs to if block
//Statement2
System.out.println(“Gifts are so special”); // This statement doesnot belong to if block
In the above example, if the condition ( isEligible ) is true , Statement1 will execute.
Statement2 runs no matter what because it is not a part of if block.
if Statement execution flow
The below diagram demonstrate the flow chart of an “if statement execution flow ” in java.
Java if-else Statement
else if statement is used to specify a new condition if the first condition is false.
Syntax:
if ( condition1 ) {
// block of code to be executed if the condition1 is true.
} else if ( condition2 ) {
// block of code to be executed if the condition1 is false and condition2 is true .
} else {
// block of code to be executed if condition1 is false and condition2 is false.
}
Real time example :
If the score is more than 90, eligible for luxurious gift. Else if the score is more than 80 and less than 90, eligible for cute gifts . Else, not eligible .
Example:
int score = 85 ;
if ( score >90 ) {
System.out.println(“Eligible for luxurious gifts”);
} else if ( 80<score<90 ) {
System.out.println(” Eligible for cute gifts “);
} else {
System.out.println(” not eligible for gifts”);
}
In the above example , since score is 85, the first condition ( score > 90 ) is not met, so the if block skipped. Then it will check the else if condition ( 80<score<90 ), which is true. That means the else if block runs and prints ” Eligible for cute gifts”
if-else statement execution flow:
The below diagram demonstrate the flow chart of an ” if-else statement execution flow” in programming.
Java nested-if statement
Java nested if describe having one if statement inside another if statement. If the outer condition is true , the inner conditions are checked and executed accordingly.
Syntax :
if ( condition 1 ) {
// Executes when condition 1 is true
if ( condition 2 ) {
//Executes when condition 2 is true
}
}
Nested if execution flow
The below diagram demonstrate the flow chart of an “nested-if statement execution flow” in java programming.
Example: The below example demonstrate the use of nested if statement to check multiple conditions.
class Gift{
public static void main(String args[]) {
int score= 100;
//Outer if statement
if (90<score<100){
System.out.println(“Eligible for gifts“);
// Nested if statement
if ( score==100 ) {
System.out.println(“Eligible for special gift“);
}
}
}
}
Java if -else-if ladder:
In Java if-else-if statement, a user can decide among multiple options. The ‘ if ‘ statement execute in the top to bottom manner. As one of the condition associated with if is true , the statement associated with if is executed and rest of the ladder is bypassed. If none of the condition is true , then the final else statement will be executed . There can be many ‘ else if’ blocks associated with ‘if’ block , but only one ‘else’ block is allowed with one ‘if’ block.
Syntax:
if(condition1 ) {
// code to be executed if condition1 is true
} else if ( condition 2 ) {
// code to be executed if the condition2 is true
}else{
//code to be executed if all the conditions are false
}
Java if-else-if ladder execution flow:
Example: The example below demonstrates an if-else-if ladder to check multiple conditions and execute the corresponding block of code based on the value of score.
class Gifts{
public static void main ( String args [] ){
int score=100;
if( 80<score<90 )
System.out.println(“Eligible for gift worth $100“);
else if (90<score<95 )
System.out.println(“Eligible for gift worth $150“);
else if ( 95 < score < 100 )
System.out.println(” Eligible for gift worth $200“);
else if ( score==100 )
System.out.println(“Eligible for gift worth $300 “);
else
System.out.println(“Not eligible for gift“);
}
}
Output
Eligible for gift worth $300
Java Switch Case:
Java switch statement is one of the decision making statement which provides a way to control program flow by executing different blocks of code based on the value of a single value expression . It is an alternative way to use java if-else-if statements when there are multiple possible conditions.
Syntax:
switch( expression ) {
case value1:
//code to be executed if expression == value1
break;
case value2:
//code to be executed if expression==value2
break;
case value3:
//code to be executed if expression==value3
// more cases…
default:
//code to be executed if no cases match
}
Switch statements execution flow
The below diagram demonstrate the flow chart of a “Switch statements execution flow ” in java programming.
Example: The below java program demonstrate the use of switch-case statement .
class Gifts {
public static void main (String args [] )
{
int score = 100;
switch ( score ) {
case 80 :
System.out.println( “Score is 80 & eligible for gift worth $ 100 “);
break;
case 85 :
System.out.println(” Score is 85 & eligible for gift worth $ 150 “);
break;
case 90 :
System.out.println(” Score is 90 & eligible for gift worth $ 200 “);
break ;
case 100 :
System.out.println(” Score is 100 & eligible for gift worth $ 300 ” );
break ;
default :
System.out.println(“Eligible for simple gifts“);
}
}
}
Output
Score is 100 & eligible for gift worth $300
NOTE:
- The expression in switch case can be of type byte, short , int , char or enumeration . From JDK7 , the expression in switch can also be of type String.
- The default statement in switch case is optional.
- Duplicate case values are not allowed in switch case.
- The break statement inside the switch case is used to terminate the statement sequence.
- Break statement in switch case is necessary because without it the execution will continue to the next case.
Java Jump Statemnents:
Java jump statements are control flow statements that transfer the sequential execution of a program from one point to the another point of the code .
Java provides three main jump statements :
break , continue and return .
- Break : In Java , the jump statement break is used for :
* To exit a loop .
* To terminate a sequence in a switch statement.
* Labeled break is used to exit a specific outer loop from within a nested structure.
- Continue : Continue statement in java is a control flow statement used within loops to skip the current iteration and immediately proceed to the next iteration. When continue statement is found , the remaining code within the current loop skip and the loops control flow jumps to the next iteration.
When to use continue statement in Java ?
Continue statement can be used in java within loops , if you want to skip the current iteration and immediately proceed to the next iteration .
*Java continue statement is used for all types of loops , but it is mostly used in for , while , and do-while loops .
*In case of for loop , the continue statement forces the control to skip the current iteration and jump immediately to the update statement.
*In case of while loop or do-while loop , the control immediately jumps to the boolean expression.
- Return statement
The return statement in Java is one of the jump statement which is used to explicitly return from a method . While using return method, program control transfer back to the caller of the method.
Comparison of Decision-Making Statements
if-else vs switch-case
The table below demonstrates the difference between if-else and switch-case in java.
| Features | if-else | switch-case |
|---|---|---|
| Definition | The if-else statement in java is a decision-making statement which allows program to execute different blocks of code based on whether a specified condition is true or false. If the condition is true , then if block execute, otherwise if false , else block execute. | The switch statement in java is a control flow statement which allows a program to execute different blocks of code based on the value of single expression . |
| Readability | if-else statements are more readable for a few conditions | Switch statements are more readable and efficient for many cases. |
| Use Case | Suitable for condition based checks. | Switch-case is best for exact value matching |
| Flexibility | if-else statement supports ranges and complex conditions. | Switch statement supports exact matches of values |
| Performance | if-else statement is slower for many checks due to multiple conditions. | Switch statement is faster and optimized for handling many cases. |
