Else-If Statement in Java
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.
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:
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:
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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
