Conditional Statement in Java

In this tutorial, we will discuss more about the Conditional statement and Loops in Java.

Objective:

  • What is Conditional Statement
  • Type of Conditional Statement
  • Example of each type of Conditional Statement

What is conditional statement in java ?

From the name, you can guess the functionality of the conditional statement in java. It will check the condition, validate the condition and based on the validation it will execute the corresponding statement.

You can ask what is statement?

In simple language, statement is nothing but a line in the java program.

Before understanding the conditional statement in details you must know about the branching in java

Branching is process when a program continues a sequential code and umped to another part of the code. Branching is of 2 types.

  • Conditional Branching
  • Unconditional Branching

In java, to achieve the conditional branching, several ways are available. Such as

  • If statement
  • Switch case
  • Conditional Operator.

Let’s discuss one by one in details.

If Statement in Java:

If Statement in java is a simple conditional statement. With if statement you can check condition and choose which line should be executed based on the condition. It has 2 direction for checking the condition. i.e. Yes or No.

If condition satisfies, then it will accept the yes condition and follow the if condition. If it does not satisfies the if condition the it will redirect to No condition and follow the else condition.

Conditional Statement in Java

From the above flow diagram you can notice that it has 2 criteria for checking the if condition. i.e. Yes or No.

Syntax

If( Expression )
{
            Statement for Yes ;
}
Statement for No

In java we have several types of if conditional statement.

  • if
  • if —– else
  • Nested if
  • if —— elseif—– else

Let’s discuss one by one in details.

If Condition:

If condition in java is a simple conditional statement. With if statement you can check condition and choose which line should be executed based on the condition. It has 2 direction for checking the condition. i.e. Yes or No.

If condition satisfies, then it will accept the yes condition and follow the if condition. If it does not satisfies the if condition the it will not do anything and exit the loop and go to the next statement of the code.

Conditional Statement in Java

Syntax:

If( Expression )
{
            Statement for Yes ;
}

Example:

class checkcondition {
	public static void main(String args[])
	{
		int a = 4;

		if (a>1)
		{
			System.out.println("If Condition Satishfied")); 
			
		}
	}
}

If —-else Condition:

Like if condition, you can check condition and choose which line should be executed based on the condition. It has 2 direction for checking the condition. i.e. Yes or No.

If condition satisfies, then it will accept the yes condition and follow the if condition. If it does not satisfies the if condition the it will redirect to No condition and follow the else condition.

Conditional Statement in Java

Syntax:

If( Expression )
{
            Statement for Yes ;
}
Else{
Statement for No
}

Example:

class checkcondition {
	public static void main(String args[])
	{
		int a = 4;

		if (a<1)
		{
			System.out.println("If Condition Satisfied")); 
			
		}
        else{
			System.out.println("Execute else block"));
	    }
	}
}

Nested if —- else:

Like if—— else condition, you can check condition and choose which line should be executed based on the condition. Here if condition can be added inside another if conditional statement. That’s why this is known as Nested if —-else.

Conditional Statement in Java

Syntax:

If( Expression )
{
            Statement for Yes ;
            If{
            Statement for nested if
}
Else{
Statement for nested else
}
}
Else{
Statement for No
}

Switch Case in Java:

If you want to check condition for each value then, Switch case id the best for use in Java. In java you can use integer or character to validate against each switch case. Each Case should be ended with a colon. If no switch case is validated then the default condition will be executed.

Syntax:

Switch ( Expression / Variable)
{
            Case Value 1;
                        Statement;
                        break;
Case Value 2;
                        Statement;
                        break;
            Case Value 3;
                        Statement;
                        break;
            Case Value 4;
                        Statement;
                        break
            default:
                        Statement;
                        break;
}

Conditional Operator:

This is one type of operator where you can check the condition. See below syntax to understand the flow.

Expression 1 ? Expression 2 : Expression 3;

This means of Expression 1 is True then Expression 2 will be executed else expression 3 will be executed.

In next section, we will discussion on Looping in Java

Leave a Reply

Your email address will not be published. Required fields are marked *