Java

Conditional Statements in Java

Conditional statements are mostly used in decision-making scenarios which means these statements take a decision on the basis of some conditions. The conditional statements are also referred as branching statements because the program takes a decision based on the result of the assessed condition. A very simple example of conditional statements from our daily life can be if today is Friday, then tomorrow will be Saturday.

This write-up demonstrates conditional statements in Java and it will be organized in the following way:

Let’s start!

if Statement in Java

It is one of the most simple and significant conditional statements which will execute only if the condition is true. It must be specified in the lowercase letters “if” and uppercase “IF” wouldn’t work as Java is a case sensitive language and hence it will generate an error. The below-given figure shows the basic syntax of “if-statement” in Java:

if (condition)
{
   statement(s); //executes if condition is true
}

The statements present in the body of the if condition will be executed only if the condition is true.

Example

Let’s consider the below piece of code that provides a detailed understanding of the if statement.

int a=75, b=100;
if (a < b)
{
   System.out.println("The value of a is less than b");
}

The above code snippet tests whether the value of “a” is less than “b”, and if it is true then it will print “The value of a is less than b“:

Since the condition is true, therefore the body of the “if” statement gets executed.

else Statement in Java

The if statement executes the code only if the condition is true and to tackle the falsy conditions an else statement will be used. Following will be the syntax for the else statement in java:

if (condition)

{
   statement(s); //executes if condition is true
}
else
{
   statement(s) //executes if condition is false
}

A condition specified in the if statement will be checked if “true” then everything that comes within the body of if-statement will be executed and if the condition is false then else statement will be executed.

Example

Let’s extend the previous example a little and specify a piece of code for the false condition as well:

int a=175, b=100;
if (a < b)
{

   System.out.println("The value of a is less than value of b");
}
else
{
   System.out.println("The value of b is less than value of a");
}

The above code-snippet provides the following output:

The above output verifies that the condition is false and the else condition is executed.

else if Statement in Java

It is used when we have to tackle more than two conditions, it determines a new condition and executes the statements if the condition specified within the “else if” statement is true. The below-given snippet shows the working of the else-if statement in java:

if (condition)
{
   statement(s); //executes if condition is true

}
else if(condition)
{
   statement(s) //executes if condition is true
}
else
{
   statement(s) //executes if condition is false
}

Example

Let’s consider an example to show ‘A’ grade if your marks are greater than 80, ‘B’ grade if the obtained marks are greater than 60 but less than 80, and it shows ‘F’ grade if your marks are less than 50:

int a=75;
if (a >= 80)
{
System.out.println("Excellent! You Got A Grade");
}
else if(a >60 && a<80)
{
System.out.println("Good.. You Got B Grade");
}
else
{

   System.out.println("You Got F Grade.. Better Luck next Time!");
}

As a=75 which meets the criteria of else if statement so, above code-snippet will provide the following output:

The output authenticates that conditional statements are working correctly.

Ternary Operator in Java

A ternary operator “?” is used to combine a condition with expressions in single line. It is an alternative method for the if-else statements but in a shorter way and as the name itself suggests, it is a combination of three instances/operands.

Syntax

The basic syntax of the ternary operator in Java is:

(condition) ? exp1: exp2

Example

The below-given snippet describes how to use ternary operator in Java:

int a=55;
String res= (a >= 50) ? "Pass" : "Fail";
System.out.println(res);

The above produces the following output:

Output authenticates the working of ternary operator.

Conclusion

The conditional statements are used to tackle the decision-making scenarios, if statement tests a condition and executes the code only if the condition is true, the else statement executes only if the condition is false, and else if can be used to specify a new condition which will execute if the condition is true. Lastly, this article demonstrates that the ternary operator is a shorthand for the if else statement.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.