Control Flow in Java – if, else, switch, and Ternary Operator


Table of Contents

  1. Introduction to Control Flow
  2. The if Statement
  3. The else and else if Statements
  4. The switch Statement
  5. The Ternary (Conditional) Operator
  6. Nested if Statements
  7. Common Mistakes and Pitfalls
  8. Real-World Use Cases
  9. Summary and What’s Next

1. Introduction to Control Flow

Control flow statements in Java allow you to control the execution flow of your program based on certain conditions. These conditions help the program make decisions, repeat actions, or perform alternate actions.

There are several control flow structures in Java:

  • Conditional statements (if, else if, else, switch)
  • Looping statements (for, while, do-while)
  • Jump statements (break, continue, return)

In this module, we will explore the if, else, switch, and the ternary operator, which are used for making decisions in the program.


2. The if Statement

The if statement is the most basic form of decision-making in Java. It allows you to execute a block of code only if a particular condition is true.

Syntax:

if (condition) {
// code to execute if condition is true
}

Example:

int number = 10;
if (number > 5) {
System.out.println("Number is greater than 5.");
}

In the above example, the condition number > 5 is true, so the message is printed.

Notes:

  • The condition inside the if statement must be a boolean expression.
  • If the condition is false, the code inside the block is skipped.

3. The else and else if Statements

The else statement can be used to specify a block of code that will execute if the if condition evaluates to false.

Syntax:

if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}

Example:

int number = 3;
if (number > 5) {
System.out.println("Number is greater than 5.");
} else {
System.out.println("Number is less than or equal to 5.");
}

The else if statement is used when you want to check multiple conditions. It allows you to specify several alternative conditions.

Syntax:

if (condition1) {
// code to execute if condition1 is true
} else if (condition2) {
// code to execute if condition2 is true
} else {
// code to execute if all conditions are false
}

Example:

int number = 7;
if (number > 10) {
System.out.println("Number is greater than 10.");
} else if (number > 5) {
System.out.println("Number is greater than 5 but less than or equal to 10.");
} else {
System.out.println("Number is 5 or less.");
}

4. The switch Statement

The switch statement is another form of decision-making. It is often used when you need to test one variable against multiple potential values.

Syntax:

switch (variable) {
case value1:
// code to execute if variable == value1
break;
case value2:
// code to execute if variable == value2
break;
default:
// code to execute if variable does not match any of the above values
}

Example:

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

Notes:

  • The break statement is used to terminate the switch statement and exit the block.
  • If the break statement is omitted, the program will “fall through” to the next case statement until a break is encountered.
  • The default case is optional and runs if none of the case conditions match.

5. The Ternary (Conditional) Operator

The ternary operator provides a compact way to perform a simple if-else decision in a single line.

Syntax:

condition ? expression1 : expression2;

If the condition is true, expression1 is executed; if the condition is false, expression2 is executed.

Example:

int number = 8;
String result = (number > 5) ? "Greater than 5" : "Less than or equal to 5";
System.out.println(result);

In the above example, the condition (number > 5) evaluates to true, so the output will be “Greater than 5.”


6. Nested if Statements

You can nest if statements inside each other to perform more complex decision-making. This is useful when you need to evaluate multiple conditions in a hierarchical manner.

Example:

int number = 12;
if (number > 10) {
if (number % 2 == 0) {
System.out.println("Number is greater than 10 and even.");
} else {
System.out.println("Number is greater than 10 but odd.");
}
} else {
System.out.println("Number is less than or equal to 10.");
}

7. Common Mistakes and Pitfalls

  • Missing break statement in switch: If you forget the break statement, the program will continue to execute all the following cases until a break is encountered, potentially leading to unintended results.
  • Misplaced parentheses: Incorrect placement of parentheses can lead to logical errors, especially when dealing with multiple conditions. Always ensure that conditions are correctly grouped.

8. Real-World Use Cases

  • Login Validation: Using if statements to validate user input in a login form. String username = "admin"; String password = "password123"; if (username.equals("admin") && password.equals("password123")) { System.out.println("Login successful."); } else { System.out.println("Invalid credentials."); }
  • Menu Selection: Using switch statements to implement menu options. int option = 2; switch (option) { case 1: System.out.println("Start game"); break; case 2: System.out.println("Load game"); break; default: System.out.println("Invalid option"); }

9. Summary and What’s Next

In this module, we covered:

  • The if, else, and else if statements for conditional logic
  • The switch statement for handling multiple conditions
  • The ternary operator for concise conditional expressions
  • Nested if statements for more complex conditions