Lesson 17. If ... Else Statements in Java | Learn Java Basics

Published on
Belongs to Category: Learn Basic Java|Posted by: Le Thanh Giang||4 min read
Facebook share iconLinkedIn share iconTwitter share iconPinterest share iconTumblr share icon
Lesson 17. If ... Else Statements in Java | Learn Java Basics

In Java programming, conditional statements play an important role in controlling the flow of the program. When writing a program, we often encounter situations where we need to check a condition and decide on an action based on the result of that check.

To accomplish this, Java provides the if ... else statement, which allows us to check a condition and execute different code blocks based on that condition.

In this article, we will explore the if ... else conditional statement in Java, its syntax, and how to use it in practice.

If Statement in Java

The if statement in Java is used to check a condition. If the condition is true, the block of code inside the if statement will be executed. If the condition is false, the program will skip the if block.

Syntax of if

if (condition) {
    // Code block will execute if condition is true
}

Example: Using if to check for a positive number

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

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

        System.out.println("The program continues...");
    }
}

Output:

The number 10 is positive.
The program continues...

If ... else Statement in Java

The if ... else statement allows you to check a condition and provide an alternative block of code to execute if the condition is false.

Syntax of if ... else

if (condition) {
    // Code block executes if condition is true
} else {
    // Code block executes if condition is false
}

Example: Checking if a number is positive or negative

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

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

        System.out.println("The program continues...");
    }
}

Output:

The number -5 is negative.
The program continues...

If ... else if ... else Statement in Java

If there are multiple conditions to check, you can use if ... else if ... else. This helps to check multiple conditions and execute the corresponding code block for the first true condition.

Syntax of if ... else if ... else

if (condition1) {
    // Execute if condition1 is true
} else if (condition2) {
    // Execute if condition1 is false but condition2 is true
} else {
    // Execute if all conditions above are false
}

Example: Determine if a number is positive, negative, or zero

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

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

Output:

The number 0 is zero.

Nested if Statements in Java

You can use one if statement inside another if statement, which is called a nested if. This is useful when you need to check multiple related conditions.

Example: Check valid age and categorize

public class NestedIfExample {
    public static void main(String[] args) {
        int age = 20;

        if (age >= 18) {
            System.out.println("You are old enough to register.");

            if (age >= 21) {
                System.out.println("You can register for an advanced class.");
            }
        } else {
            System.out.println("You are not old enough to register.");
        }
    }
}

Output:

You are old enough to register.

Some Notes When Using if ... else

  • The condition in the if statement must be a boolean value (true or false).
  • Use curly braces {} when there is more than one statement inside an if or else block.
  • Avoid nesting too many if statements within each other to make the code easier to read.
  • Use the ternary operator if you have a simple condition to check.

Conclusion

The if ... else conditional statement is one of the most important tools in Java for controlling the flow of the program. By using it, you can create more flexible and intelligent programs.

Now, practice writing some programs using if ... else to better understand how it works!

Next article: Article 18. Practice: Solving quadratic equations with Java

Latest Posts

Related Posts

Newsletter border

Subscribe to Receive Updates from RiverLee