![]() |
![]() |
Java if statement | |
Java if-else statement | |
Java nested if-else | |
Java else-if ladder | |
Java Switch case statement | |
Java Nested Switch Case | |
Java Ternary/Conditional operator |
Java while loop | |
Java do-while loop | |
Java for loop | |
Java Enhanced loop/for each loop | |
Java Labelled for loop | |
Java Nesting of loops | |
Java Break and Continue |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
Nesting means writing one statement into another statement.Nested if else allows us to write one if else inside another if else.
Nesting is required when we want to check another condition inside one condition.
if ( condition1 ) { condition1 statement; if( sub-condition ) { sub-condition statement; } else { sub-condition else statement; } } else { condition1 else statement; }
First condition1 is checked, if it is true then control enteres inside block and again check for the sub-conditions same as normal if else
If condition1 becomes false then control jumps to the else block and execute it.
Finding the largest number from given three numbers using nested if else
public class FindThreeGreatestEx { public static void main(String[] args) { int first = 30, second = 20, third = 50; if(first>second) { if(first>third) System.out.println("Greater is "+first); else System.out.println("Greater is "+third); } else { if(second>third) System.out.println("Greater is "+second); else System.out.println("Greater is "+third); } } }