How Do You Write a Nested If Statement in Java

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. Live Demo. … Output. X = 30 and Y = 10.

Can you do nested if statements in Java?

Yes, java allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement.

What is the function of nested if in Java?

When there is an if statement within another if statement it is known as a nested if statement. There are times when we test a condition within another condition and return one value if the condition is met and return another value if the condition is not met.

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest