Module: Control Flow in Java

if-else statements

Java Core: Control Flow - if-else Statements

if-else statements are fundamental building blocks for controlling the flow of execution in Java programs. They allow your code to make decisions based on whether a condition is true or false.

Basic Syntax:

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

Explanation:

  • if (condition): This part checks the condition. The condition must be a boolean expression (something that evaluates to true or false).
  • { ... } (if block): If the condition is true, the code inside these curly braces is executed.
  • else { ... } (else block): If the condition is false, the code inside these curly braces is executed. The else block is optional.

Example 1: Simple if-else

int age = 20;

if (age >= 18) {
  System.out.println("You are an adult.");
} else {
  System.out.println("You are a minor.");
}

Output:

You are an adult.

Example 2: if without else

int number = 5;

if (number > 10) {
  System.out.println("Number is greater than 10.");
}
// If number is not greater than 10, nothing happens.

Output:

(No output, because the condition number > 10 is false.)

if-else if-else Chains:

You can chain multiple if-else if statements together to check for multiple conditions.

Syntax:

if (condition1) {
  // Code to execute if condition1 is true
} else if (condition2) {
  // Code to execute if condition1 is false AND condition2 is true
} else if (condition3) {
  // Code to execute if condition1 and condition2 are false AND condition3 is true
} else {
  // Code to execute if all conditions are false (optional)
}

Example 3: if-else if-else Chain

int score = 85;

if (score >= 90) {
  System.out.println("Grade: A");
} else if (score >= 80) {
  System.out.println("Grade: B");
} else if (score >= 70) {
  System.out.println("Grade: C");
} else {
  System.out.println("Grade: D");
}

Output:

Grade: B

Important Considerations:

  • Curly Braces: While not required for single-statement blocks, it's highly recommended to always use curly braces {} for clarity and to prevent errors when you add more statements later.

    int x = 10;
    if (x > 5)  // Technically valid, but bad practice
      System.out.println("x is greater than 5");
    
    int y = 10;
    if (y > 5) { // Best practice - always use braces
      System.out.println("y is greater than 5");
      System.out.println("This line will also execute");
    }
    
  • Boolean Expressions: The condition in an if statement must evaluate to a boolean value (true or false). You can use comparison operators (==, !=, >, <, >=, <=) and logical operators (&& (AND), || (OR), ! (NOT)) to create complex boolean expressions.

  • Nested if Statements: You can nest if statements inside other if or else blocks to create more complex decision-making logic. However, excessive nesting can make your code difficult to read and maintain.

Example 4: Nested if Statement

int age = 25;
boolean hasLicense = true;

if (age >= 18) {
  System.out.println("You are eligible to drive.");
  if (hasLicense) {
    System.out.println("You have a valid license.  You can drive!");
  } else {
    System.out.println("You need to obtain a driver's license.");
  }
} else {
  System.out.println("You are too young to drive.");
}

Output:

You are eligible to drive.
You have a valid license.  You can drive!

if-else statements are a cornerstone of Java programming. Understanding how to use them effectively is crucial for writing programs that can respond to different situations and make intelligent decisions.