Decision Making in Java – Explore the types of Statements with Syntax
Each programming language provides some constructs which are necessary for decision making and also for the flow of control of a program. Decision making is really important when we get trapped in some dilemma.
Java language provides the facility of decision-making with the use of selection statements, which depend on expression’s truth value. If the condition is true, control goes into a particular block of code, and if it is false, then the control goes to another block of the program.
In this article, we will cover each and every decision-making statement that is supported by Java. Before starting our Java Tutorial on Decision Making in Java, you first need to take a quick revision of Data types in Java.
Decision Making in Java
Decision making in Java is done with the help of selection statements or selection constructs. The selection construct means the flow of execution of statement(s) depends upon a test-condition.
If a condition is fulfilled, a course-of-action (a set of statements) is followed otherwise, another course-of-action (a different set of statements) is followed. This selection construct is also called the decision construct because it helps in decision-making about which set-of-statements are to be executed.
The following figure explains the selection or decision construct:
Types of Decision Making Statements in Java
Java provides two types of decision-making statements:
- if statement
- switch statement
The if statement can be further divided into many categories that we will cover in this article. We will also discuss the jump statements in Java, which are break and continue. Let’s start talking about each decision statement with syntax and examples.
1. The if statement in Java
An if statement checks a particular condition; if the condition evaluates to true, it will execute a statement or a set of statements. Otherwise, if the condition is false, it will ignore that statement or set of statements. The test expression of if must be of boolean type.
The general form or syntax of the if statement is:
if( test-condition) { //statement(s); }
Here, the statement can be a single statement, a compound statement or even an empty statement.
If we do not provide the curly braces ‘{‘ and }’ after the if statement then by default it will consider the immediate single statement after the if statement.
For example,
if( test-condition) //statement1; //statement2;
In the above code, if the condition is true then the if statement considers the only statement1 to be inside the if-block.
The following figure shows the if construct of Java.
Code to illustrate the if statement:
package com.TechVidvan.DecisionMakingDemo; //Java program to illustrate If statement public class IfStatementDemo { public static void main(String args[]) { int i = 5; if (i % 2 == 0) System.out.println("5 is divisible by 2"); // This statement will be executed // as if considers one statement by default System.out.println("I am Not in if block"); } }
Output:
Caution!! Do not put a semicolon after the test condition in the if statement. If you do so, the if statement will end there. The block of statements following the if statement will no longer be the part of if in such cases.
2. The if-else statement in Java
The if statement only allows us to execute a set of statements if a condition or expression is true. What if there is another course of action to be followed if the expression evaluates to false? So, there comes another form of if that allows for this kind of either-or condition by providing an else clause.
It is called an if-else statement in which we can use the else statement with an if statement so that we can execute a block of code when the test condition is false.
The general form or syntax of the if-else statement is:
if( test-condition) { //statement(s); } else { //statement(s); }
Note: Remember, in an if-else statement, only the code associated with the if or the code associated with else executes, both can never execute together.
The following figure shows the if-else construct of Java.
Code to illustrate the if-else statement:
package com.TechVidvan.DecisionMakingDemo; //Java program to illustrate If-else statement public class IfElseStatementDemo { public static void main(String args[]) { int i = 5; if (i % 2 == 0) System.out.println("5 is divisible by 2"); else System.out.println("5 is not divisible by 2"); } }
Output:
3. The Nested-Ifs statements in Java
A nested if is an if statement that has another if in its if’s body or in its else’s body. As Java allows nested if statements, we can place an if or else-if statement inside another if statement.
The general form or syntax of the Nested if statement can either be in 3 forms:
if(expression1) { : if(expression2) statement1; else statement2 ; : } else body of else;
if(expression1) { body of if ; else { : if(expression2) statement1; else statement2; }
if(expression1) { : if(expression2) statement1; else statement2 ; : } else { if(expression3) statement3; else statement4; }
The following figure shows the Nested if construct of Java.
Code to illustrate the Nested if statement:
package com.TechVidvan.DecisionMakingDemo; //Java program to illustrate Nested If statement public class NestedIfDemo { public static void main(String args[]) { int age = 18, weight = 50 ; if (age >= 18) { System.out.println("You are eligible to vote"); if (weight >= 50) System.out.println("You are eligible to vote and donate blood"); else System.out.println("you are not eligible to donate blood") ; } else System.out.println("you are not eligible for both!!") ; } }
Output:
You are eligible to vote and donate blood
4. The if-else-if Ladder Statement of Java
The if-else-if ladder is a very common programming constructs in Java, which is also called the if-else-if staircase because of its appearance. We can use many if-else-if statements in our program.
The general form or syntax of the if-else-if ladder statement is:
if( expression1) statement1 ; else if(expression2) statement2; . . else statement3;
The following figure shows the if-else-if ladder of Java.
Code to illustrate the if-else-if ladder statement:
package com.TechVidvan.DecisionMakingDemo; //Java program to illustrate Nested If statement public class IfElseIfDemo { public static void main(String[] args) { int number=-13; if(number>0) { System.out.println("The number is POSITIVE"); } else if(number<0) { System.out.println("The number is NEGATIVE"); } else { System.out.println("The number is equal toZERO"); } } }
Output:
5. The switch statement of Java
A switch statement is a multiple-branch statement in Java. The switch statement successively checks the value of an expression with a list of integer or character constants.
The data type of expression in a switch must be byte, char, short or int. When a match is found, the statements associated with that constant are executed.
The general form or syntax of the switch statement is:
switch (expression) { case value1: statement1; break; case value2: statement2; break; . . case valueN: statementN; break; default: statementDefault; }
The switch statement evaluates the expression and matches the values against the constant values which are specified in the case statements.
When a match is found, the statement associated with that case is executed until it encounters a break statement or else the switch statement ends. When no match is found, the default statement gets executed.
In the absence of break, the control flow moves to the next case below the matching case and this is called fall through.
Note: The default statement is optional and if it is missing, no action takes place if all matches fail.
The following figure shows the switch case of Java.
Code to illustrate the switch statement:
package com.TechVidvan.DecisionMakingDemo; //Java program to illustrate switch statement public class SwitchCaseDemo { public static void main(String[] args) { int day = 5; switch(day) { case 0: System.out.println("Sunday"); break; case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; default: System.out.println("Invalid"); break; } } }
Output:
6. Jump statements in Java
The jump statement unconditionally transfers the flow of the program within a function.
Java provides three types of jump statements which are:
- return
- break
- continue
Out of these three, you can use the return statement anywhere in the program whereas break and continue are used inside the smallest block of code like loops, etc.
6.1. The break Statement:
The break statement enables a program to skip over a part of the code. A break statement is generally used to terminate the loops like while, do-while, for and a switch statement. The execution resumes at the statement immediately after the terminated statement.
Syntax of break statement in loops:
while(expression1) { statement1; if(expression2) break; //From here the control goes to statement3 : statement2; } statement3; for(int ; expression1 ; update) { statement1; if(expression2) break; //From here the control goes to statement3 : statement2; } statement3; do { statement1; if(expression2) break; //From here the control goes to statement3 : statement2; } while(expression); statement3;
The following figure shows the break statement of Java.
Code to illustrate break statement:
package com.TechVidvan.DecisionMakingDemo; public class BreakStatementDemo { //Java program to illustrate using break to exit a loop public static void main(String args[]) { int iterator = 0; // Initially loop is set to run from 0 to 9 while(iterator < 10) { System.out.println("iterator: " + iterator); iterator++; // loop will terminate when iterator is 5. if (iterator == 5) break; } System.out.println("Loop complete."); } }
Output:
iterator: 1
iterator: 2
iterator: 3
iterator: 4
Loop complete.
6.2. The continue Statement:
The continue is another jump statement similar to the break statement. It also skips over a part of the code, like the break statement.
But the continue statement is different from the break statement in a way that instead of terminating from the loop, it goes to the next iteration of the loop, skipping any code in between.
The continue statement skips the rest of the loop statements and causes the next iteration of the loop.
Syntax of continue statement in loops:
while(expression1) { statement1; if(expression2) continue; //From here the control goes to expression1 of while loop : statement2; } statement3; for(int ; expression1 ; update) { statement1; if(expression2) continue; //From here the control goes to update expression in for loop : statement2; } statement3; do { statement1; if(expression2) continue; //From here the control goes to expression1 of while loop : statement2; } while(expression); statement3;
The following figure shows the continue statement of Java.
Code to illustrate the use of continue statement:
package com.TechVidvan.DecisionMakingDemo; public class ContinueStatementDemo { //Java program to illustrate using the continue to exit a loop public static void main(String args[]) { int iterator = 0; // Initially loop is set to run from 0 to 10 while(iterator <= 10) { iterator++; // If the number is odd, skip and continue if (iterator %2 == 1) continue; // If the number is odd,print it System.out.println("iterator: " + iterator); } System.out.println("Loop complete."); } }
Output:
iterator: 4
iterator: 6
iterator: 8
iterator: 10
Loop complete.
6.3. The return Statement:
The return statement is used to return from a function or a method. This statement is useful in two ways. First, it is used to immediately exit from the function.
The second use of return is that it returns a value to the calling code. The compiler will bypass or skip every statement after the encounter of a return statement.
Code to illustrate return statement:
package com.TechVidvan.DecisionMakingDemo; public class ReturnStatementDemo { //Java program to illustrate using return statement to exit a function public static void main(String args[]) { int iterator = 0; // Initially loop is set to run from 0 to 10 while(iterator <= 10) { iterator++; // If the number is odd, print it if (iterator %2 == 1) System.out.println("iterator: " + iterator); return; //the remaining code will be skipped } System.out.println("Loop complete."); } }
Output:
Summary
The decision-making statements are useful for easy coding in Java. These statements are necessary for deciding what to do in response to changing conditions. In this Java article, we covered each of the decision making statements along with their syntax and code examples.
We also learned the concept of jump statements which will help you with efficient programming. This article will surely help you in building and strengthening your concepts on decision making in Java.
Thank you for reading our article. Don’t forget to share your feedback through the comment section below.