If-else Statement in Java

by Jasleen Chhabra | Updated on 24 August 2024

Table of Contents (Click any Topic to view first)

  1.  If-else Statement Syntax:
  2. Example 1: Simple If-else Statement
  3. Example 2: Nested If-else Statement
  4. Example 3: Chained If-else Statement (if-else-if)
  5. Example 4: If-else Statement with User Input

In Java programming, decision-making statements allow you to control the flow of your program based on specific conditions. One of the fundamental decision-making statements in Java is the if-else statement. This statement evaluates a condition and executes different blocks of code depending on whether the condition is true or false. Let's explore the if-else statement in detail with examples.

If-else Statement Syntax:

if (condition) {

    // Code block executed if condition is true

} else {

    // Code block executed if condition is false

}


Example 1: Simple If-else Statement

public class IfElseExample {

    public static void main(String[] args) {

        int x = 10;

        if (x % 2 == 0) {

            System.out.println("x is even");

        } else {

            System.out.println("x is odd");

        }

    }

}

In this example, if the value of x is divisible by 2 (i.e., it's even), the program prints "x is even." Otherwise, it prints "x is odd."


Example 2: Nested If-else Statement

public class NestedIfElseExample {

    public static void main(String[] args) {

        int age = 25;

        if (age >= 18) {

            if (age <= 30) {

                System.out.println("Age is between 18 and 30");

            } else {

                System.out.println("Age is greater than 30");

            }

        } else {

            System.out.println("Age is less than 18");

        }

    }

}

This example demonstrates a nested if-else statement. If the age is greater than or equal to 18, it checks if the age is also less than or equal to 30. Based on these conditions, it prints different messages.


Example 3: Chained If-else Statement (if-else-if)

public class ElseIfExample {

    public static void main(String[] args) {

        int marks = 85;

        if (marks >= 90) {

            System.out.println("Grade: A");

        } else if (marks >= 80) {

            System.out.println("Grade: B");

        } else if (marks >= 70) {

            System.out.println("Grade: C");

        } else {

            System.out.println("Grade: D");

        }

    }

}

In this example, the program evaluates the marks and assigns a grade based on the score. It uses chained if-else-if statements to check multiple conditions in sequence.


Example 4: If-else Statement with User Input

import java.util.Scanner; 

public class UserInputExample {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter your age: ");

        int age = scanner.nextInt();

        if (age >= 18) {

            System.out.println("You are eligible to vote");

        } else {

            System.out.println("You are not eligible to vote");

        }

        scanner.close();

    }

}

This example demonstrates how to use the if-else statement with user input. It prompts the user to enter their age and then determines whether they are eligible to vote based on the entered age.

Conclusion:

The if-else statement is a powerful tool in Java for making decisions in your programs. Whether it's simple conditions, nested scenarios, or chained evaluations, the if-else statement provides flexibility and control over the flow of your code. Understanding its syntax and usage is essential for writing efficient and effective Java programs.



FAQ

Any Questions?
Look Here.

Related Articles

Abstraction in Java

Binding in Java

Break Statement in Java

Classes & Objects in Java

Collections Framework in Java

Comments in Java

Continue Statement in Java

Control Statements in Java

Data Types in Java

Do While-Loop in Java

Encapsulation in Java

Exception Handling in Java

For-Loop In Java

Hello World Program in Java

Inheritance in Java

Introduction to Java

Java Database Connectivity (JDBC)

Java Development Tools and Frameworks

Java GUI (Graphical User Interface) Programming

Java I/O

Java Vs. C++

Methods and Constructors in Java

Multithreading in Java

Object Oriented Programming in Java

Operators in Java

Polymorphism in Java

Scanner Class in Java

Setting Up Java Environment

Static Keyword in Java

Super Keyword in Java

Switch Statement in Java

This Keyword in Java

Variables in Java

While-Loop in Java