IF ELSE IF control Statement in Java

Understanding the "if else if" Control Statement in Java

Control statements are an essential part of programming languages and play a crucial role in determining the flow of execution in a program. If some problem we have to check many conditions to be checked in this scenario if and if else will not work. For this a new control statement is required in which is called as if else if control flow statement. In this the control flow starts with if condition and ends with else block. In between if , else one or more than one else if blocks will be present. Each else if block consists of one condition statement if the condition is ture then the purticular else if block will be executed else next subsequent else if block condtion will be checked. if none of the else if conditons are true then else block will be executed.

Basic Syntax of the "if else" Statement

The basic syntax of the "if" statement in Java is as follows:


if (condition1) { // code block to be executed if the condition is true
}
else if(condition2 ){
// code block to be executed
}
else if(condition3 ){
// code block to be executed
}
... else{
//code
}

The condition1, condition2, condition3 in the above syntax is a Boolean expression that evaluates to either true or false. For example, consider the following code snippet:

Example Program to demonstrate if else if control statement

Program to find given number is even or odd using if control statement


Output 1


Output 2