- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the ‘if...else if...else’ statement in Java and how to use it?
An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.
When using if, else if, else statements there are a few points to keep in mind.
- An if can have zero or one else's and it must come after any else if's.
- An if can have zero to many else if's and they must come before the else.
- Once an else if succeeds, none of the remaining else if's or else's will be tested.
Syntax
if(Boolean_expression 1) { // Executes when the Boolean expression 1 is true }else if(Boolean_expression 2) { // Executes when the Boolean expression 2 is true }else if(Boolean_expression 3) { // Executes when the Boolean expression 3 is true }else { // Executes when the none of the above condition is true. }
Example
public class Test { public static void main(String args[]) { int x = 30; if( x == 10 ) { System.out.print("Value of X is 10"); }else if( x == 20 ) { System.out.print("Value of X is 20"); }else if( x == 30 ) { System.out.print("Value of X is 30"); }else { System.out.print("This is else statement"); } } }
Output
Value of X is 30
- Related Articles
- What is the ‘if else’ statement in Java and how to use it?
- Java if-else statement
- Java if-else-if ladder statement
- Java if-then-else statement
- What is if...else if... statement in JavaScript?
- What is the if...else statement in JavaScript?
- How to use ‘else if ladder’ conditional statement is C language?
- How to use if...else statement at the command line in Python?
- IF ELSE statement in a MySQL Statement?
- What is basic syntax of Python if...else statement?
- What is the syntax of Python if...elif...else statement?
- How to indent an if...else statement in Python?
- Explain if-else statement in C language
- What is the ‘if’ statement in Java and how to use it?
- Explain Nested if-else statement in C language

Advertisements