Menu
Coddy logo textTech

Nested if-Statement

Lesson 5 of 11 in Coddy's Control Statements in Java course.

Instead of using multiple if statements, we can also use else if along with if, thus forming an if-else-if-else ladder or just an if-if ladder.

The nested if statement represents an if block within another if block. Here, the inner if block condition executes only when the outer if block condition is true.

Syntax:

if(condition){    
    //code to be executed, else optional   
    if(condition){  
        //code to be executed, else optional   
    }    
} 
challenge icon

Challenge

Easy

Create a function that takes two int variables, age and weight, and returns a string value.

Conditions:
If age is greater than 18 and weight is greater than 50, then print "Valid"; otherwise, print "Invalid".

Try it yourself

class DonateBlood {
    public static String donateBlood(int age, int weight) {
        // Write code here
    }
}

All lessons in Control Statements in Java

1What are Control Statements?

Introduction