Java Fundamentals

Decision Making Statements

Prepared By Krishna Srikanth M

1. Decision Making

Decision-making structures have one or more conditions to be evaluated or tested by the program. Statements are executed when a condition is true and, optionally, other statements are executed when the condition is false.

The general idea is:

Condition
Condition True
Execute Conditional Code
Continue

Java provides several decision-making statements:

  • if
  • if...else
  • if...else if...else
  • Nested if...else
  • switch
Decision-making overview and if flow diagram from source PDF

2. if Statement

An if statement consists of a Boolean expression followed by one or more statements.

Syntax

if(Boolean_expression) {
    // Statements will execute if the Boolean expression is true
}

If the Boolean expression evaluates to true, the block of code inside the if statement is executed. If it evaluates to false, execution continues with the first set of code after the if statement.

Example

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

        if( x < 20 ) {
            System.out.print("This is if statement");
        }
    }
}

Output

This is if statement.
Key point: An if statement executes its block only when its Boolean condition is true.

3. if...else Statement

An if statement can be followed by an optional else statement. The else block executes when the Boolean expression is false.

Syntax

if(Boolean_expression) {
    // Executes when the Boolean expression is true
} else {
    // Executes when the Boolean expression is false
}

If the Boolean expression evaluates to true, the if block is executed; otherwise, the else block is executed.

Example

public class Test {
    public static void main(String args[]) {
        int x = 30;

        if( x < 20 ) {
            System.out.print("This is if statement");
        } else {
            System.out.print("This is else statement");
        }
    }
}

Output

This is else statement
if else section from source PDF

4. The if...else if...else Statement

An if statement can be followed by optional else if and else statements. This is useful for testing multiple conditions.

Important Rules

  • An if can have zero or one else, and the else must come after any else if blocks.
  • An if can have zero to many else if blocks, and they must come before the else.
  • Once an else if condition succeeds, the remaining else if and else blocks are not tested.

Syntax

if(Boolean_expression 1) {
    // Executes when Boolean_expression 1 is true
} else if(Boolean_expression 2) {
    // Executes when Boolean_expression 2 is true
} else if(Boolean_expression 3) {
    // Executes when Boolean_expression 3 is true
} else {
    // Executes when none of the above conditions is true
}

Example

public class Test {
    public static void main(String args[]) {
        int x = 30;

        if( x == 10 ) {
            System.out.print("Value of X is 10");
        } else if( x == 20 ) {
            System.out.print("Value of X is 20");
        } else if( x == 30 ) {
            System.out.print("Value of X is 30");
        } else {
            System.out.print("This is else statement");
        }
    }
}

Output

Value of X is 30
Execution: Java checks the conditions from top to bottom and executes the first matching block.
if else if else syntax and example from source PDF

5. Nested if...else

It is legal to nest if and else if statements. This means one conditional statement can appear inside another conditional statement.

Syntax

if(Boolean_expression 1) {
    // Executes when Boolean_expression 1 is true

    if(Boolean_expression 2) {
        // Executes when Boolean_expression 2 is true
    }
}

else if...else can also be nested in a similar way.

Example

public class Test {
    public static void main(String args[]) {
        int x = 30;
        int y = 10;

        if( x == 30 ) {
            if( y == 10 ) {
                System.out.print("X = 30 and Y = 10");
            }
        }
    }
}

Output

X = 30 and Y = 10
Nested if else example from source PDF

6. switch Statement

A switch statement allows a variable/expression to be tested for equality against a list of values. Each value is called a case.

Syntax

switch(expression) {
    case value:
        // Statements
        break; // optional

    case value:
        // Statements
        break; // optional

    // You can have any number of case statements.

    default: // Optional
        // Statements
}

How switch Works

The switch expression is evaluated. Java compares its value with the available case labels. When a matching case is found, its statements execute. A break can terminate the switch; without a break, execution can fall through into subsequent cases.

switch flow diagram and example from source PDF

7. Rules of a switch Statement

RuleDescription
Allowed switch typesThe source lists integers, convertible integer types such as byte, short, char, strings and enums.
Number of casesAny number of case statements can be used.
Case valueA case is followed by the value to compare and a colon.
Matching typeThe case value must correspond to the switch expression's type and be a constant/literal.
Matching caseWhen the switch value equals a case, statements following that case execute.
breakWhen reached, break terminates the switch and control moves to the next statement after it.
Fall-throughIf no break appears, execution continues into subsequent cases until a break is reached.
defaultOptional case used when none of the other cases matches. It normally appears at the end.
Important: A break is optional in an individual case, but omitting it intentionally or accidentally can cause fall-through behavior.

8. switch Example

public class Test {
    public static void main(String args[]) {

        // char grade = args[0].charAt(0);
        char grade = 'C';

        switch(grade) {
            case 'A':
                System.out.println("Excellent!");
                break;

            case 'B':
            case 'C':
                System.out.println("Well done");
                break;

            case 'D':
                System.out.println("You passed");

            case 'F':
                System.out.println("Better try again");
                break;

            default:
                System.out.println("Invalid grade");
        }

        System.out.println("Your grade is " + grade);
    }
}

Output

Well done
Your grade is C
Notice: Cases 'B' and 'C' share the same statement block. Because grade is 'C', the program prints Well done and then exits the switch at the following break.
switch flow diagram and complete grade example from source PDF

9. Quick Revision

StatementPurpose
ifExecutes a block when a Boolean condition is true.
if...elseChooses between two blocks based on a condition.
if...else if...elseTests multiple conditions from top to bottom.
Nested ifPlaces one conditional statement inside another.
switchTests an expression against multiple case values.
caseDefines a possible matching value inside switch.
breakTerminates switch execution and prevents further fall-through.
defaultRuns when none of the cases matches.

Decision-Making Flow

Condition
   |
   +---- true ----> Execute matching block
   |
   +---- false ---> Check next condition / else
                         |
                         +---- switch default if no case matches