Java nested-if statement with Example

A programming language uses control statements to control the flow of execution of a program. In Java programming, we can control the flow of execution of a program based on some conditions. Java control statements can be put into the following three categories: selection, iteration, and jump.



Java nested-if Statement:

Java nested-if statement means an if statement inside another if statement. A nested-if is an if statement that is the target of another if or else statement. In nested-if, the inner if block condition executes only when outer if block condition is true. 

Syntax: 

if(condition1){
//code to be executed for condition1
}
if(condition2){
//code to be executed for condition2
}



Flowchart:

Java nested-if statement

Java nested-if Example:


class NestedIfExample {
    public static void main(String[] args) {
        int num1 = 10, num2 = 5, num3 = 20, largestNumber;
        if (num1 >= num2) {
            if (num1 >= num3) {
                largestNumber = num1;
            } else {
                largestNumber = num3;
            }
        } else {
            if (num2 >= num3) {
                largestNumber = num2;
            } else {
                largestNumber = num3;
            }
        }
        System.out.println("Largest number is: " + largestNumber);
    }
}


Output:
 Largest number is: 20 



You may also like these related posts:

1. Java if-else statement with example.
2. Java if statement with example.
3. Types of operators in java with example.
4. Java variables with examples.
5. Basic data types in Java with example.
6. Top 100 Java interview question and answer.
7. Java collection framework complete tutorial.
8. Java Encapsulation with Example.
9. Java Polymorphism with example.
10. Java inheritance with example.
Java nested-if statement with Example Java nested-if statement with Example Reviewed by Prashant Srivastava on December 16, 2019 Rating: 5

No comments:

Powered by Blogger.