- 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
Java nested if statement example
It is always legal to nest if-else statements which means you can use one if or else if statement inside another if or else if statement.
Syntax
The syntax for a nested if...else is as follows −
if(Boolean_expression 1) { // Executes when the Boolean expression 1 is true if(Boolean_expression 2) { // Executes when the Boolean expression 2 is true } }
Example
You can nest else if...else in the similar way as we have nested if statement.
public class Test { public static void main(String args[]) { int x = 30; int y = 10; if( x == 30 ) { if( y == 10 ) { System.out.print("X = 30 and Y = 10"); } } } }
Output
This will produce the following result −
X = 30 and Y = 10
- Related Articles
- Java if statement example
- Java switch statement example
- What is the ‘nested if’ statement in Java and how to use it?
- How to use nested if statement in Python?
- Explain Nested if-else statement in C language
- Java if statement
- Java if-else statement
- Java if-else-if ladder statement
- Java if-then-else statement
- Example of a nested table in HTML
- Nested Classes in Java
- Nested interface in Java
- Static Nested Classes in Java
- IF ELSE statement in a MySQL Statement?
- Java switch statement

Advertisements