(Translated by https://www.hiragana.jp/)
Java if-else - GeeksforGeeks
Open In App

Java if-else

Last Updated : 02 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

Decision-making in Java helps to write decision-driven statements and execute a particular set of code based on certain conditions. The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. In this article, we will learn about Java if-else.

If-Else in Java

If- else together represents the set of Conditional statements in Java that are executed according to the condition which is true.

Syntax of if-else Statement

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

Java if-else Flowchart

Java if-else flowchart

if-else Program in Java

Dry-Run of if-else statements

1. Program starts.
2. i is initialized to 20.
3. if-condition is checked. 20<15, yields false.
4. flow enters the else block.
  4.a) "i is greater than 15" is printed
5. "Outside if-else block" is printed.

Below is the implementation of the above statements:

Java
// Java program to illustrate if-else statement

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

        if (i < 15)
            System.out.println("i is smaller than 15");
        else
            System.out.println("i is greater than 15");

        System.out.println("Outside if-else block");
    }
}

Output
i is greater than 15
Outside if-else block

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:

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

Below is the implementation of Nested if statements:

Java
// Java Program to implementation
// of Nested if statements

// Driver Class
public class AgeWeightExample {
      // main function
    public static void main(String[] args) {
        int age = 25;
        double weight = 65.5;
      
        if (age >= 18) {
            if (weight >= 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.

Frequently Asked Questions

What is the if-else in Java?

If-else is conditional statements that execute according to the correct statement executed. If the if condition is true then the code block inside the if statement is executed else it executes the else statement block.

What is the syntax for if-else?

The syntax for if-else:

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

Previous Article
Next Article

Similar Reads

Java if-else-if ladder with Examples
Decision Making in Java helps to write decision-driven statements and execute a particular set of code based on certain conditions.Java if-else-if ladder is used to decide among multiple options. The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is ex
3 min read
Decision Making in Java (if, if-else, switch, break, continue, jump)
Decision Making in programming is similar to decision-making in real life. In programming also face some situations where we want a certain block of code to be executed when some condition is fulfilled. A programming language uses control statements to control the flow of execution of a program based on certain conditions. These are used to cause t
7 min read
Writing clean if else statements
Using if else chaining some time looks more complex, this can be avoided by writing the code in small blocks. Use of conditional statement increases the code readability and much more. One best practice should be handling error case first. Below shown example shows how to handle error cases and simplify the if else logic.Examples 1: updateCache()-
5 min read
switch vs if else
Prerequisite - Switch Statement, Decision making(if else) A switch statement is usually more efficient than a set of nested ifs. Deciding whether to use if-then-else statements or a switch statement is based on readability and the expression that the statement is testing. Check the Testing Expression: An if-then-else statement can test expressions
3 min read
Output of C programs | Set 65 (If-Else)
Prerequisite : Decision making in CQuestion 1 C/C++ Code #include&quot;stdio.h&quot; #include&quot;stdlib.h&quot; void reverse(int i) { if (i &gt; 5) exit(0); printf(&quot;%d\n&quot;, i); return reverse(i++); } int main() { reverse(1); } OPTIONS: a)Segmentation fault b)Compilation error c)Print 1 Infinite time d)Both a &amp; c OUTPUT: (d)Both a
2 min read
Difference Between java.sql.Time, java.sql.Timestamp and java.sql.Date in Java
Across the software projects, we are using java.sql.Time, java.sql.Timestamp and java.sql.Date in many instances. Whenever the java application interacts with the database, we should use these instead of java.util.Date. The reason is JDBC i.e. java database connectivity uses these to identify SQL Date and Timestamp. Here let us see the differences
7 min read
Java AWT vs Java Swing vs Java FX
Java's UI frameworks include Java AWT, Java Swing, and JavaFX. This plays a very important role in creating the user experience of Java applications. These frameworks provide a range of tools and components for creating graphical user interfaces (GUIs) that are not only functional but also visually appealing. As a Java developer, selecting the righ
11 min read
Java.io.ObjectInputStream Class in Java | Set 2
Java.io.ObjectInputStream Class in Java | Set 1 Note : Java codes mentioned in this article won't run on Online IDE as the file used in the code doesn't exists online. So, to verify the working of the codes, you can copy them to your System and can run it over there. More Methods of ObjectInputStream Class : defaultReadObject() : java.io.ObjectInpu
6 min read
Java.lang.Class class in Java | Set 1
Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It has no public constructor. Class objects are cons
15+ min read
Java.lang.StrictMath class in Java | Set 2
Java.lang.StrictMath Class in Java | Set 1More methods of java.lang.StrictMath class 13. exp() : java.lang.StrictMath.exp(double arg) method returns the Euler’s number raised to the power of double argument. Important cases: Result is NaN, if argument is NaN.Result is +ve infinity, if the argument is +ve infinity.Result is +ve zero, if argument is
6 min read
Practice Tags :
three90RightbarBannerImg