Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java if...else Statements

In Java, the if...else statement is a fundamental control flow structure that allows you to execute different blocks of code based on certain conditions. In this tutorial, we'll discuss the basics of if...else statements in Java and provide examples of their usage.

  • Basic if statement

An if statement checks if a specified condition is true. If the condition is true, the code inside the if block will be executed.

Example:

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

        if (number > 0) {
            System.out.println("The number is positive.");
        }

        System.out.println("This line is always executed.");
    }
}

In this example, the if statement checks if the number variable is greater than 0. If the condition is true, the program prints "The number is positive."

  • if...else statement

The if...else statement allows you to execute one block of code if a condition is true and another block of code if the condition is false.

Example:

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

        if (number > 0) {
            System.out.println("The number is positive.");
        } else {
            System.out.println("The number is negative.");
        }
    }
}

In this example, the if...else statement checks if the number variable is greater than 0. If the condition is true, the program prints "The number is positive." Otherwise, it prints "The number is negative."

  • if...else if...else statement

You can use multiple if...else if...else statements to check multiple conditions in a single control structure.

Example:

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

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

In this example, we use if...else if...else to check if the number variable is positive, negative, or zero. Depending on the condition, the program prints the appropriate message.

In conclusion, the if...else statement in Java is a fundamental control flow structure that allows you to execute different blocks of code based on certain conditions. Understanding the basics of if...else statements and knowing how to use them effectively is essential for writing clear and concise code.

  1. Using if...else for conditional branching in Java

    The basic structure of an if...else statement:

    int x = 10;
    
    if (x > 0) {
        System.out.println("Positive");
    } else {
        System.out.println("Non-positive");
    }
    
  2. Nested if...else statements in Java

    You can nest if...else statements for more complex conditions:

    int x = 10;
    int y = 5;
    
    if (x > 0) {
        if (y > 0) {
            System.out.println("Both x and y are positive");
        } else {
            System.out.println("x is positive, but y is non-positive");
        }
    } else {
        System.out.println("x is non-positive");
    }
    
  3. Chaining multiple conditions in if...else in Java

    Use logical operators to chain multiple conditions:

    int age = 25;
    
    if (age >= 18 && age <= 35) {
        System.out.println("Age is between 18 and 35");
    } else {
        System.out.println("Age is outside the specified range");
    }
    
  4. Java switch statement vs if...else

    Use a switch statement for multiple equal comparisons:

    int day = 3;
    
    switch (day) {
        case 1:
            System.out.println("Monday");
            break;
        case 2:
            System.out.println("Tuesday");
            break;
        // ... other cases
        default:
            System.out.println("Unknown day");
    }
    
  5. Ternary operator as a concise if...else in Java

    The ternary operator is a concise form of if...else:

    int x = 10;
    String result = (x > 0) ? "Positive" : "Non-positive";
    System.out.println(result);
    
  6. Handling multiple scenarios with if...else if...else in Java

    Use else if for multiple scenarios:

    int score = 75;
    
    if (score >= 90) {
        System.out.println("A");
    } else if (score >= 80) {
        System.out.println("B");
    } else if (score >= 70) {
        System.out.println("C");
    } else {
        System.out.println("F");
    }
    
  7. Java if...else statements in control flow and logic

    If...else statements play a crucial role in controlling the flow of your program and implementing logical decisions.