Open In App

Java if-else Statement

Last Updated : 03 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The if-else statement in Java is a powerful decision-making tool used to control the program’s flow based on conditions. It executes one block of code if a condition is true and another block if the condition is false. In this article, we will learn Java if-else statement with examples.

Example:

Java
// Java Program to demonstrate 
// if-else statement
public class IfElse {
  
    public static void main(String[] args) {
      
        int n = 10;

        if (n > 5) {
            System.out.println("The number is greater than 5.");
        } else {
            System.out.println("The number is 5 or less.");
        }
    }
}

Output
The number is greater than 5.

Dry-Running the Above Example:

  1. Program starts.
  2. n is initialized to 10.
  3. if condition is checked: 10 > 5, yields true.
    • Block inside the if statement executes: "The number is greater than 5." is printed.
  4. The else block is skipped as the condition is true.

Syntax of if-else Statement

if (condition) { // Executes this block if // condition is true } else { // Executes this block if // condition is false }

Working of if-else Statement

  1. Control falls into the if block.
  2. The flow jumps to the condition.
  3. The condition is tested:
    1. If the condition yields true, go to Step 4.
    2. If the condition yields false, go to Step 5.
  4. The if block or the body inside the if is executed.
  5. If the condition is false, the else block is executed instead.
  6. Control exits the if-else block.

Flowchart of Java if-else Statement

Below is the Java if-else flowchart.

Flowchart of Java if-else

In the above flowchart of Java if-else, it states that the condition is evaluated, and if it is true, the if block executes; otherwise, the else block executes, followed by the continuation of the program.

Nested if statement in Java

In Java, we can use nested if statements to create more complex conditional logic. Nested if statements are if statements inside other if statements.

Syntax of Nested if

if (condition1) { // code block 1 if (condition2) { // code block 2 } }

Example:

Java
// Java Program to implement
// Nested if statement
public class NestedIf {
 
    public static void main(String[] args) {
      
        int a = 25;
        double w = 65.5;
      
        if (a >= 18) {
            if (w >= 50.0) {
                System.out.println("You are eligible to donate blood.");
            } else {
                System.out.println("You must weigh at least 50 kilograms to donate blood.");
            }
        } else {
            System.out.println("You must be at least 18 years old to donate blood.");
        }
    }
}

Output
You are eligible to donate blood.

Note: The first print statement is in a block of “if” so the second statement is not in the block of “if”. The third print statement is in else but that else doesn’t have any corresponding “if”. That means an “else” statement cannot exist without an “if” statement.

Java if-else Statement – FAQs

What is the if-else Statement in Java?

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

What is the Syntax for if-else Statement?

if (condition) { // Executes this block if // condition is true } else { // Executes this block if // condition is false }

Can an else Block Exist without an if Statement?

No, an else block must always be associated with an if statement. It cannot exist independently.

What Happens if the if Condition is false and there is no else Block?

If the if condition evaluates to false and there is no else block, the program skips the if block and continues execution with the next statement.

Can We Use Multiple if-else Statements in a Program?

Yes, we can use multiple if-else statements in a program, and they can also be nested to handle complex decision-making.



Next Article

Similar Reads

Practice Tags :
three90RightbarBannerImg