Home  >  Article  >  Java  >  Else-If Statement in Java

Else-If Statement in Java

王林
王林Original
2024-08-30 15:23:171019browse

Conditional statements used to check if a block of code is to be executed or not is called else-if statements. If a specified condition is true, it is executed or executes the condition given in the else block of the code. This block code is used to test if a condition is true or not so that the following codes can be executed. Else statement block is optional. Also, there is if-else-if statements and nested if statements. Only one else can be used with an if condition. This is one of the basic statements in any programming language.

Syntax

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The syntax generally used for the Else If the statement is like a ladder where if one statement is not executed, the other statement is executed. If the multiple checks do not execute all the Else If statements, the Else statement is finally executed, giving a specific output. The syntax of the Else If statement is given below:

Code:

if(condition1)
{
//Specific code to be run if the Condition 1 is true according to the program.
}
else if(condition2)
{
// Specific code to be run if the Condition 2 is true according to the program
}
else if(condition3)
{
// Specific code to be run if the Condition 3 is true according to the program
}
...
else
{
// Specific code to be run if the Condition n is true according to the program false
}

In the above syntax, we notice that if none of the conditions is executed, then the final Else statement is executed, which is the nth condition. The syntax is extremely similar to the If statement. The difference is that there are multiple Ifs in the Else If statement.

Flowchart of Else-If Statement in Java

The flowchart of the Else If statement is very similar to the If statement. We can check the working of the Else If statement with a flowchart. As shown in the diagram, if the Condition 1 is false, then the Condition 2 is executed. If that is also false, then the Condition 3 is executed, and so on.
On the other hand, if the condition 1 is true, then Statement 1 is executed. Also, if Condition 1 is false, then it moves to Condition 2, and if the Condition 2 is true, then Statement 2 is executed.

Else-If Statement in Java

Examples of Else-If Statement in Java

Here are the following examples of Else-If Statement in Java mention below

Example #1

In the first coding example, we are going to enter a number and check whether it is positive, negative or zero. We used the Else if ladder in this case and check the behavior of the number. It is a very basic program, finding the nature of the number.

Code:

import java.io.*;
public class PositiveNegativeExample
{
public static void main(String[] args)throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a number");
int n= Integer.parseInt(br.readLine());
if(n>0)
{
System.out.println("The number is POSITIVE");
}
else if(n<0)
{
System.out.println("The number is NEGATIVE");
}
else
{
System.out.println("The number is ZERO");
}
}
}

Output:

Else-If Statement in Java

Else-If Statement in Java

In the coding example 1. we first input 36 as the number, and then we enter 0 as a number. We get the perfect output, respectively. When we enter 36 as the number, we get the output that the number is positive. Again, we enter a number as zero, and then we get the output that the number is zero.

Example #2

In this coding example, we check the Else If statement’s functioning, and we see whether a person is eligible to donate blood or not. We do not use Buffered Reader for the input of the two variables.  We directly input them into the program, and we get the result as desired.

Java Program to illustrate the working of Else If Statement

Code:

public class Age
{
public static void main(String[] args)
{
//Here the variable a is age and w is weight
int a=25;//Age
int w=48;// Weight
//Generating condition on age and weight
if(a>=18){
if(w>50)
{
System.out.println("You are eligible to donate blood");
} else
{
System.out.println("You are not eligible to donate blood");
}
} else
{
System.out.println("Age must be greater than 18");
}
}
}

Output:

Else-If Statement in Java

In the sample code, we input the age as 25 and weight as 48, and we execute the program accordingly. The age is greater than 18, so it satisfies the condition to donate blood. However, the weight is less than 50, which is required in the program, so the program rejects the person to donate blood.

Example #3

In this program, we check the grade of a student according to the marks entered by the user. The grades are Fail, D, C, B, A, and A+.

Java Program to check the grade of a student in a particular exam as entered by the user.

Code:

import java.io.*;
public class Exam
{
public static void main(String[] args)throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter marks of the student in the exam");
int m=Integer.parseInt(br.readLine());
if(m<50)
{
System.out.println("The student has failed");
}
else if(m>=50 && m<60)
{
System.out.println("The student has got D grade");
}
else if(m>=60 && m<70)
{
System.out.println("The student has got C grade");
}
else if(m>=70 && m<80)
{
System.out.println("The student has got B grade");
}
else if(m>=80 && m<90)
{
System.out.println("The student has got A grade");
}
else if(m>=90 && m<100)
{
System.out.println("The student has got A+ grade");
}
else{
System.out.println("Invalid!");
}
}
}

Output:

Else-If Statement in Java

Else-If Statement in Java

From the program, we enter 65 and 80 as the numbers. The program successively returns that the student has got a C grade and A grade in the exam, respectively.

Conclusion

In this article, we check the functionality of the Else If statement in Java, and we see that it is nothing but a multiple If statement which is used in all the programs. We also see three coding examples that enlighten the function of the Else if statement very elaborately. All the programs use Else If statements extensively and print the output in a fashion desired by the user. Further, Else if statement is used wherever there are multiple conditions to be checked. They are used in all programming languages.

The above is the detailed content of Else-If Statement in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Control Statement in JavaNext article:Control Statement in Java