Skip to content

Decision Making Statements

In Java, decision-making statements are used to execute a block of code based on certain conditions. These statements control the flow of execution in the program by evaluating boolean expressions (true/false conditions). Java provides several types of decision-making statements:


1. if Statement

  • Purpose: Executes a block of code if the condition evaluates to true.
  • Syntax:

if (condition) {

    // Code to execute if condition is true

}

Example:

public class IfExample {

    public static void main(String[] args) {

        int number = 10;

        if (number > 0) {

            System.out.println(“The number is positive.”);

        }

    }

}


2. if-else Statement

  • Purpose: Executes one block of code if the condition is true and another block if it is false.
  • Syntax:

if (condition) {

    // Code to execute if condition is true

} else {

    // Code to execute if condition is false

}

Example:

public class IfElseExample {

    public static void main(String[] args) {

        int number = -5;

        if (number > 0) {

            System.out.println(“The number is positive.”);

        } else {

            System.out.println(“The number is not positive.”);

        }

    }

}


3. if-else-if Ladder

  • Purpose: Used to check multiple conditions sequentially. If one condition is true, the corresponding block of code is executed, and the rest are skipped.
  • Syntax:

if (condition1) {

    // Code for condition1

} else if (condition2) {

    // Code for condition2

} else {

    // Code if all conditions are false

}

Example:

public class IfElseIfExample {

    public static void main(String[] args) {

        int marks = 85;

        if (marks >= 90) {

            System.out.println(“Grade: A”);

        } else if (marks >= 80) {

            System.out.println(“Grade: B”);

        } else if (marks >= 70) {

            System.out.println(“Grade: C”);

        } else {

            System.out.println(“Grade: F”);

        }

    }

}


4. switch Statement

  • Purpose: Tests a variable for equality against a set of values (cases) and executes the matching block.
  • Syntax:

switch (expression) {

    case value1:

        // Code for case value1

        break;

    case value2:

        // Code for case value2

        break;

    default:

        // Code if no cases match

}

Example:

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;

            default:

                System.out.println(“Invalid day”);

        }

    }

}

Key Points for switch:

  • The break statement prevents fall-through to subsequent cases.
  • The default case is optional but recommended for unmatched cases.
  • From Java 12 onwards, a switch expression is introduced that can return a value.

Switch Expression Example (Java 12+):

public class SwitchExpressionExample {

    public static void main(String[] args) {

        int day = 3;

        String dayName = switch (day) {

            case 1 -> “Monday”;

            case 2 -> “Tuesday”;

            case 3 -> “Wednesday”;

            default -> “Invalid day”;

        };

        System.out.println(“Day: ” + dayName);

    }

}


Nested Decision-Making Statements

  • Purpose: Decision-making statements can be nested inside other decision-making statements for more complex conditions.

Example:

public class NestedIfExample {

    public static void main(String[] args) {

        int age = 25;

        int weight = 70;

        if (age > 18) {

            if (weight > 50) {

                System.out.println(“Eligible to donate blood.”);

            } else {

                System.out.println(“Not eligible to donate blood due to weight.”);

            }

        } else {

            System.out.println(“Not eligible to donate blood due to age.”);

        }

    }

}


Comparison of if and switch

Featureif Statementswitch Statement
ConditionEvaluates any boolean expressionWorks with specific values (int, char, String, enums)
ComplexitySuitable for complex conditionsSuitable for simple equality checks
PerformanceSlower for many conditionsFaster for many discrete cases

Key Points

  1. Use if-else:
    1. When conditions are based on ranges or complex expressions.
  2. Use switch:
    1. When comparing a single variable against discrete values.
  3. Avoid Fall-through in switch:
    1. Use break statements to prevent unintended execution of subsequent cases.
  4. Readable Code:
    1. Ensure decision-making logic is clear and comments explain the conditions.