Decision-making statements are the core of programming logic, making your code dynamic and responsive. They allow your program to take different actions based on conditions. From simple if and if-else statements to more advanced structures like the if-else-if ladder, nested if, and switch, these tools help you make decisions in your code effectively. Let’s dive in and see how they work!
Decision-Making Statements
Flow Control Statements
These are the statements that control the flow of the execution of a program. Flow control statements are essential programming tools that help manage the execution flow of your Java program. They dictate the sequence in which various statements, instructions, or function calls are carried out. With these statements, you can:
- Make decisions based on specific conditions
- Repeat code blocks multiple times
- Navigate to different sections of the program
- Effectively manage complex program logic.
There are three ways to control flow, by using.
Conditional/Decision-Making Statements
Decision-making statements in Java allow you to control the flow of program execution based on certain conditions. These statements enable your program to make decisions and execute different blocks of code accordingly.
Types of Conditional/Decision-Making Statements
There are five types of Decision-Making Statements:
- if Statement
- If else Statement
- If-else-if ladder Statement
- Nested if Statement
- Switch Statement
1. If Statement
- It is a Conditional/Decision-making statement and it controls the flow based on the condition.
- The if statement is the most basic decision-making statement. It executes a block of code only if the specified condition is true.
- if comes with a condition and it is associated with a true statement block (TSB).
- True statement block (TSB) will store the statements that should be executed whenever the condition is true.
Syntax:
if (Condition) {
//Statements
}
//next line
JavaExample:
public class Student {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
}
}
Explanation:
- The condition age >= 18 is checked.
- Since the value of age is 20, which is greater than 18, the condition evaluates to true.
- The message “You are eligible to vote.” is printed.
- If the age had been less than 18, the code inside the if block would have been skipped.
2. If-else Statement
- It is a Conditional/Decision-making statement that controls the flow of execution of the program.
- The if-else statement provides an alternative action if the condition is false.
- The if comes with a condition and the condition is associated with a True statement block (TSB), else comes with a False statement block (FSB).
- In true statement block (TSB) we need to store the instructions which are exhibited whenever the condition is true.
- In a false statement block (FSB) we need to store the instructions that are executed whenever the condition is false.
Syntax:
if (Condition) {
//Statements
}else{
//Statements
}
//next line
JavaExample:
public class Student {
public static void main(String[] args) {
int age = 16;
if (age >= 18) {
System.out.println("You are eligible to vote.");
} else {
System.out.println("You are not eligible to vote.");
}
}
}
Explanation:
- The condition age >= 18 is checked.
- Since the age is 16, the condition evaluates to false.
- The code inside the else block is executed, printing “You are not eligible to vote.”
- This ensures that either the if block or the else block is executed, but not both.
3. if-else-if Ladder Statement
- It is a Conditional/Decision-making statement that controls the flow of execution of the program.
- The if comes with a condition and the condition is associated with the True statement block (TSB 1), else if comes with multiple conditions and each condition is associated with its own task (TSB).
- If any one of the conditions is true then it executes its True Statement Block (TSB) and the JVM goes to the next line (skips the next conditions).
- If all the conditions are false then the else block will execute, here the else block is not mandatory.
Syntax:
if (Condition 1) {
//Statements
}else if (Condition 2){
//Statements
}else if (Condition 3){
//Statements
}
.
.
.
else if (Condition n){
//statements when all the above condition are false and condition n is true
}else{
//statements when all the above condition are false
}
//next line
JavaExample:
public class Student {
public static void main(String[] args) {
int marks = 65;
if (marks > = 90) {
System.out.println("Grade: O");
} else if (marks >= 75) {
System.out.println("Grade: A");
} else if (marks >= 60) {
System.out.println("Grade: B");
} else {
System.out.println("Grade: C");
}
}
}
Explanation:
- The program checks the first condition, marks >= 90. It evaluates to false since 65 < 90.
- It then checks the second condition, marks >= 75, It evaluates to false since 65 < 75.
- It then checks the third condition, marks >= 60, which evaluates to true.
- The message “Grade: B” is printed.
- The remaining else block is skipped because one condition has already been satisfied.
- The if-else-if ladder helps simplify situations where you need to test several conditions in sequence.
4. Nested-if Statement
- It is a Conditional/Decision-making statement that controls the flow of execution of the program.
- You can place an if statement inside another if statement to create nested conditions.
- It’s used when you need to check a condition only after another condition is true.
Syntax:
if (Condition) {
if (Condition){
//Statements
}
}else{
//Statements
}
}else {
if (Condition){
//Statements
}
}else{
//Statements
}
}
//next line
JavaExample:
public class NestedIfExample {
public static void main(String[] args) {
int number = 15;
if (number > 0) {
if (number % 2 == 0) {
System.out.println("The number is +ve and even.");
} else {
System.out.println("The number is +ve but odd.");
}
} else {
if (number < 0) {
System.out.println("The number is -ve.");
} else {
System.out.println("The number is zero.");
}
}
}
}
Explanation:
- Outer if (number > 0)
- Check if the number is greater than 0 (positive).
- If true, the program evaluates the inner if (number % 2 == 0) condition.
- If false, the program moves to the outer else block.
- Inner if (number % 2 == 0) in Outer if Block
- Executes only if number > 0 is true and the number is divisible by 2 (even).
- Prints: “The number is positive and even.”
- Inner else in Outer if Block
- Executes only if number > 0 is true but the number is not divisible by 2 (odd).
- Prints: “The number is positive but odd.”
- Outer else Block
- Executes if number <= 0.
- Inside this block:
- Inner if (number < 0): Handles cases where the number is negative.
- Prints: “The number is negative.”
- Inner else: Handles cases where the number is zero.
- Prints: “The number is zero.”
- Inner if (number < 0): Handles cases where the number is negative.
5. Switch Statement
- It is a Conditional/Decision-making statement that controls the flow of execution of the program.
- The switch statement is a more readable way to check a variable against multiple fixed values, it is mainly used for multiple comparisons. The switch statement allows you to select one of many code blocks to be executed based on a value.
- In this Expression/Variable types and the case type should be the same.
- No case data should be repeated once again.
- If the variable/Expression didn’t match any case, the default block would execute.
Syntax:
switch (Variable/Expression) {
case condition 1:
//Statements
break;
case condition 2:
//Statements
break;
.
.
.
case condition n:
//Statements
break;
default;
//Statements
}
//next line
JavaExample:
public class Student {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
}
}
}
Explanation:
- The value of the day is 3.
- The program matches this value with case 3 and executes the corresponding block, printing “Wednesday”.
- The break statement prevents execution from continuing to the next cases.
- If the value of the day didn’t match any case, the default block would execute, printing “Invalid day”.
Summary
In this article, we explored the key conditional statements in Java:
- if for simple conditions.
- if-else for alternative actions.
- if-else-if ladder for multiple conditions.
- Nested if for checking conditions within conditions.
- switch for comparing a single variable against multiple fixed values.
By mastering these statements, you can write more dynamic and intelligent Java programs.
Must Read
Looping Statements in Java
– Master the art of iteration with for, while, and do-while loops in Java.
Jumping Statements in Java
– Understand how to alter the flow of loops using break, continue, and return statements in Java.
Keywords in Java
– Learn about the reserved keywords in Java and their specific functions.
Java User Input
– Learn how to handle user input in Java using Scanner class and other methods.
Operators in Java
– Get to know the various operators in Java and how to use them in expressions.
FAQ’S
What are conditional statements in Java?
Conditional statements allow you to make decisions in your code by testing conditions and executing different code blocks based on the results.
When should I use switch over if-else?
Use a switch when comparing a single variable against multiple fixed values for better readability.
Can I use multiple conditions in an if statement?
Yes, you can combine conditions using logical operators like && (AND) and || (OR).
Example:
if (age > = 18 && isCitizen) {
System.out.println("Eligible to vote.");
}
What is the difference between if and switch?
The if statement is flexible and can evaluate conditions involving ranges or logical operators. The switch statement is better suited for comparing a single variable against fixed values.
Can I have an if statement without an else?
Yes, an if statement doesn’t require an else. You can use it alone when you only need to handle one condition.
Is the default case in a switch mandatory?
No, the default case is not mandatory, but it’s a good practice to include it to handle unexpected values.