In this article, you will learn about the decision making statements in Java, its uses and how to use them with some examples.
Decision making statements in Java
As the name says, decision making statements are used to make decisions in critical situation in executing a program. It helps to write decision driven statements and execute a particular block of code based on the required conditions. Java supports the following decision making statements.
- if statement
- if-else statement
- if-else-if ladder
- nested if statement
- switch statement
- conditional operator
Let’s discuss them in detail
if statement
The if statement in Java is the simplest decision-making statement. The Java if statement is used to test the condition. In simple, if a certain condition is met, then it executes a specific block of code. Otherwise, executes another block code.
Syntax
if(condition) { // Statements to execute if the condition is true }
Sample program using If.. statement in Java
Input
public class Main{ public static void main(String[] args) { int age=20; if(age>18){ System.out.print("Age is greater than 18, checked using if statement."); } } }
Output
Age is greater than 18, checked using if statement.
if…else statement
The if-else statement in Java like the if statement, it also tests the condition. It executes the ‘if block of codes’ if the condition is true otherwise ‘else block of codes’ will be executed. In simple, the else statement executes when the Boolean expression is false.
Syntax:
if (condition) { // Executes this block if the condition is true } else { // Executes this block if the condition is false }
Sample program using if…else statement in Java
Input
public class Main { public static void main(String[] args) { int number = 57; if (number > 10) { System.out.println("The number is greater than 10."); } else { System.out.println("The number is lesser than 10."); } } }
Output
The number is greater than 10.
Nestedif statement
Java allows us to use multiple if or if else statements inside another if statements. But on a condition, the inner if block condition executes only when outer if block condition is true.
Syntax
if(condition){ //code to be executed if the condition is met if(condition){ //code to be executed if the condition is met } }
Sample program using Nestedif statement in Java
Input
public class Main { public static void main(String[] args) { int age=21; int weight=58; if(age>=18){ if(weight>60){ System.out.println("You are eligible to donate blood"); } else{ System.out.println("Sorry, you are not eligible to donate blood"); } } else{ System.out.println("You must be of legal age "); } } } }
Output
Sorry, you are not eligible to donate blood
Switch statement in Java
A switch statement allows a variable to be tested for equality against a list of values. The switch statement is a multi-way case statement. The switch statement allows us to execute a particular block of code based on the condition,among many alternatives(cases). The switch statement works with almost all data types byte, short, int, long, enum types and String. With the latest version of Java, you can use strings in the switch statement.
Syntax
switch (expression) { case value1: // code to be executed if expression is equal to value1 break; case value2: // code to be executed if expression is equal to value2 break; ... ... default: // default statements }
Sample program using switch statements in Java
Input
int day = 5; switch (day) { 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; case 7: System.out.println("Sunday"); break; }
Output
Friday
Conditional Operator ( ? : )
Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions.
Syntax
variable x = (expression) ? value if true : value if false
Sample program for a conditional operator in Java
Input
class Java { public static void main(String[] args) { int februaryDays = 29; String result; result = (februaryDays == 28) ? "Not a leap year" : "Leap year"; System.out.println(result); } }
Output
Leap year