Decision Structures In Java


Introduction

Controlling the flow of a program is essential in programming, and decision structures, also known as conditional statements, play a significant role in achieving this. By testing a condition, decision structures determine which section of code should be executed next. These conditions often use comparison and logical operators combined with variables and values to perform evaluations.

The If Statement

The if statement is the most basic version of a decision structure in Java. This statement evaluates a condition and executes a section of code only when the condition evaluates to true.

// Syntax for the if statement:
if (condition) {
    // Code to be executed when the condition is true
}

// Example:
if (inputValue == 0) {  
    System.out.println("Sorry, please enter a value which is not zero.");  
}

The Else Statement

The else statement is used in addition to the if statement to specify a section of code that will run when the if condition evaluates to false.

// Syntax for the else statement:
if (condition) {
    // Code to be executed when the condition is true
} else {
    // Code to be executed when the condition is false
}

// Example:
int currentHour = 15; // 3 PM

// Determine what kind of greeting to send depending on the time of day
if (currentHour < 12) {
    System.out.println("Good afternoon!");
} else {
    System.out.println("Good morning!");
}

The Else If Statement

The else if statement extends the if-else structure by allowing you to test multiple conditions. Like the if statement, it will execute a section of code where the condition evaluates to true.

// Syntax for the else if statement:
if (condition1) {
    // Code to be executed when condition1 is true
} else if (condition2) {
    // Code to be executed when condition1 is false and condition2 is true
} else {
    // Code to be executed when neither of the above conditions are true
}

// Example:
int totalMarks= 85; 

// Determine a student’s final grade 
if (totalMarks >= 90) {  
    System.out.println("Grade: A");  
} else if (totalMarks >= 75) {  
    System.out.println("Grade: B");  
} else {  
    System.out.println("Grade: C");  
}

The Switch Statement

When implementing decision structures, there are times when you need to check multiple conditions using several else-if statements. However, an alternative and often cleaner approach is the switch statement. This structure allows you to evaluate a single expression against multiple possible values and execute a specific block of code depending on the result. The switch statement is especially useful when you have a fixed set of possible outcomes, making your code more organized and easier to read.

// Syntax for the switch statement:
switch (expression) {
    case value1:
        // Code to be executed when the expression matches value1
        break;
    case value2:
        // Code to be executed when the expression matches value2
        break;
    default:
        // Code to be executed when the expression does not match a value
        break;
}

// Example:
// Customer discount percentage is determined by their loyalty tier
switch (loyaltyTier) {
    case "Bronze":
        discount = 5; 
        break;
    case "Silver":
        discount = 10; 
        break;
    case "Gold":
        discount = 15; 
        break;
    default:
        System.out.println("No loyalty tier found: No discount available.");
}

What is a Ternary Operator?

A ternary operator provides a shorthand way of writing an if-else statement. It condenses the decision-making process into a single line of code, improving readability in simple cases.

// Syntax for a ternary operator:
condition ? resultIfTrue : resultIfFalse; 


// Example:
int daysBorrowed = 15;  

// Determine if the book is overdue using a ternary operator  
String borrowingStatus = (daysBorrowed > 14) ? "Book is overdue" : "Book is within the borrowing period";  
System.out.println(borrowingStatus);

Conclusion

Understanding decision structures is a vital aspect of Java programming, allowing you to write code that responds dynamically to various conditions. From the foundational if statements to the streamlined efficiency of ternary operators, these tools enable greater control and adaptability in your programs. By mastering decision structures, you’re building the skills necessary to create robust and logical code.