Java

How to Use Java One Line if Statement?

In Java, an if statement allows users to execute the code block depending on a particular condition. The one-line if statement, also known as the ternary operator, is a concise way to write an if statement that executes one line of code based on a condition. The one-line if statement is a useful tool for writing compact and concise code in Java.

This guide will offer to use the one-line if statement with practical implementation.

How to Use Java One Line if Statement?

The one-line if statement can be used as shorthand for simple if-else statements where only a single statement is executed in each case.

Syntax

The syntax for a one-line if statement is as follows:

condition_statement? expression1: expression2

In the above syntax, first, the “condition_statement” is computed, and if it is true, the “expression1” is run; otherwise, “expression2” is run.

Example: 1

An example is considered to assign a particular number to the variable and check if the value of the variable is less or greater than the number. Here is an example of a one-line if statement:

class marks{
    public static void main(String[] args) {
        int num = 5;
        String message = (num < 10)? "Number is less than 10": "Number is greater than or equal to 10";
        System.out.println(message);
    }
}

The description of the above code is mentioned below:

  • First, assign a numeric number 5 to the num variable.
  • After that, check if the value of the variable “num” is less than ten.
  • If the above condition is true, the message “Number is less than 10” is assigned to the message variable.
  • Otherwise, the message “Number is greater than or equal to 10” is assigned to the message variable.

The output of the above code shows that “Number is less than 10”.

Example: 2

Here is another example that shows how a one-line if statement can be used to assign a value to a variable based on a condition:

class marks{
    public static void main(String[] args) {
        int num = 7;
        int result = (num > 5)? 10: 5;
        System.out.println(result);
    }
}

The explanation of the above snippet is mentioned below:

  • Initially, the number 7 is assigned to the num variable.
  • After that, check if the value of the num variable is greater than 5.
  • If the above condition is true, the value 10 is assigned to the result variable, and if it is not, the value 5 is assigned to the result variable.

The output shows that the above condition is true and the value 10 has been displayed.

Conclusion

In Java, the one-line if statement is a compact way of executing an exact single line based on the specific condition. In addition, it simplifies and clarifies the code. The syntax of the one-line if statement is easy to understand and can be used in various scenarios where only a single statement is executed based on a condition. However, it should be used judiciously, as using it excessively can make the code harder to read and understand.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.