Control Flow Statements: If-Else and Switch

Learn how to control the flow of execution using conditional statements like if-else and switch. Implement decision-making logic in your programs.


Control Flow Statements: If-Else and Switch in Java

Control flow statements are fundamental building blocks in any programming language, including Java. They allow you to control the order in which your code is executed, enabling you to create programs that make decisions and respond to different conditions. This document focuses on two key control flow statements in Java: if-else and switch.

If-Else Statements

The if-else statement is a conditional statement that allows you to execute a block of code if a certain condition is true, and optionally execute a different block of code if the condition is false.

Syntax

 if (condition) {
            // Code to be executed if the condition is true
        } else {
            // Code to be executed if the condition is false
        } 

The condition is a boolean expression that evaluates to either true or false. If the condition is true, the code inside the first block (within the first set of curly braces) is executed. If the condition is false, the code inside the else block (within the second set of curly braces) is executed. The else block is optional.

You can also chain multiple if-else statements together using else if. This allows you to check multiple conditions in sequence.

 if (condition1) {
            // Code to be executed if condition1 is true
        } else if (condition2) {
            // Code to be executed if condition1 is false and condition2 is true
        } else {
            // Code to be executed if both condition1 and condition2 are false
        } 

Example: Determining if a number is positive, negative, or zero

 public class IfElseExample {
                public static void main(String[] args) {
                    int number = 10;

                    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 zero.");
                    }
                }
            } 

Switch Statements

The switch statement is another conditional statement that allows you to select one of several code blocks to execute based on the value of a variable or expression. It's often used when you have multiple possible values and want to perform different actions for each value.

Syntax

 switch (expression) {
            case value1:
                // Code to be executed if expression equals value1
                break;
            case value2:
                // Code to be executed if expression equals value2
                break;
            // ... more cases
            default:
                // Code to be executed if expression doesn't match any of the cases
                break;
        } 

The expression is evaluated, and its value is compared to the value associated with each case. If a match is found, the code block associated with that case is executed. The break statement is crucial; it terminates the switch statement and prevents the execution of subsequent case blocks. If no break statement is present, the code will "fall through" to the next case, which is usually not desired. The default case is optional and is executed if the expression doesn't match any of the case values.

The expression must evaluate to a primitive data type such as int, char, byte, short, String (since Java 7) or an enum type.

Example: Determining the day of the week based on a number

 public class SwitchExample {
                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;
                        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;
                        default:
                            System.out.println("Invalid day number.");
                            break;
                    }
                }
            } 

Implementing Decision-Making Logic

By using if-else and switch statements, you can implement complex decision-making logic in your Java programs. These statements allow you to write code that responds to different inputs and situations, making your programs more flexible and dynamic.

Choosing between if-else and switch depends on the specific situation. Use if-else when you have complex boolean conditions or when you need to check ranges of values. Use switch when you have a single variable or expression that you want to compare against multiple specific values.

By mastering these control flow statements, you'll be well-equipped to build powerful and versatile Java applications.