if-else Statements in Java 

if-else Statements in Java

if-else statements in programming allow developers to make decisions and control the flow of code based on specified conditions. Java offers different if-else statement structures:

  1. If statement 
  2. if-else statement
  3. if-else-if ladder
  4. Nested if statement
  5. Nested if-else statement

 if Statement

The simplest form of a conditional statement is the if statement. It executes a block of code if a given condition is true.

 if Statement

Like : In the example below, you can see the message  “You are an adult!!!” will be printed in the console because condition age >= 18 is true. If the condition is false like age=12 then the message will not be printed.

int age = 25;

if (age >= 18) {
    System.out.println("You are an adult!!!");
}

if-else Statement

When you want to execute some conditional base code then this if-else statement is useful. This if-else statement can be used, If you want to execute some piece of code when condition is true and other piece of code when condition is false.

The if-else Statement

Like : In the example below, you can see the message  “You have cleared your examination!!” on the console only when condition marks >= 33 is true. If the condition is false (marks=31) then the message  “You have not cleared your examination!!” will be printed on the console.

int marks = 40;

if (marks >= 33) {
    System.out.println("You have cleared your examination!!");
} else {
    System.out.println("You have not cleared your examination!!");
}

if-else-if Ladder

When you want to execute multiple possible condition base code then this if-else-if statement is useful.  It allows you to evaluate and execute code based on different conditions in a sequential manner. This approach is used in case of multiple conditions in such a way like If one condition is not satisfied then check for other condition and so on.

The if-else-if Ladder

Like : In the example below,  you can see multiple conditions will execute one after another till the condition is not satisfied. Since marks =75, and the satisfying condition is marks >= 70,  the printed message on the console will be “Good!”

  1. In the first ‘if’ condition checking for marks,  if marks greater than or equal to 90 print message “Excellent!” otherwise 
  2. Will proceed further in ‘else if’ condition and check for condition, if marks greater than or equal to 80 print message “Very good!” otherwise
  3. Will proceed further in ‘else if’ condition and check, if marks greater than or equal to 70 print message “Good!” otherwise
  4. Print message “ You need to improve.”

So Output will be : Good!

int marks = 75;

if (marks >= 90) {
    System.out.println("Excellent!");
} else if (marks >= 80) {
    System.out.println("Very good!");
} else if (marks >= 70) {
    System.out.println("Good!");
} else {
    System.out.println("You need to improve.");
}

Nested if Statements

Sometimes, you might need to implement the logic which has conditions within conditions. This is where nested if statements come into play. If the parent condition is true only in that case the nested child condition will be executed, otherwise controle will come out of the condition(s) block. You can apply (n times)  multiple numbers of nested if conditions. 

Nested if Statements

Like : In the example below,  you can see multiple ‘nested if’ conditions are applied to verify country, state, city and pincode. In a country there could be multiple states, further a state can have multiple cities and a city can have area wise pincode. Since we are checking conditions one inside another, so if the parent condition is false it will not go inside.

As in Given program condition

  1.  Given country=”India”  which is making the condition true,  so it prints the message  “Belong to India! “ and check for state condition.
  2.  Given state=”UP”  which is making the ‘nested if’ condition true,  so it prints the message  “India UP state!“ and check for city condition.
  3. Given city=”xyz” but actual city is “abc” so conditions become false. Control will not go inside this if block and come out of all the conditions and will not print anything. 
  4. PinCode condition will not be executed since picode condition is inside “city” condition block, and city condition is not valid.

So output of this program will be

Belong to India! 

India UP state!

String country = "India";
    	String state = "UP";
    	String city = "xyz";
    	String pinCode = "234121";
    	
    	if(country.equalsIgnoreCase("India")) {
    		System.out.println("Belong to India! ");
    		
    		if(state.equalsIgnoreCase("UP")) {
    			System.out.println("India UP state!");
    			
    			if(city.equalsIgnoreCase("abc")) {
        			System.out.println("India UP state, in city abc.");
        			
        			if(pinCode.equalsIgnoreCase("111111")) {
        				System.out.println("India UP state, in city abc, pincode 111111 .");
            		}
        		}
    		}
    		
    	}

Nested if-else Statements

It is similar to the Nested if statement. Only difference is that in the Nested if-else statement,  ‘else’ condition can also be applied with nested if conditions. 

You can apply ‘nested if else’ conditions inside if block or else block. 

To keep ‘Nested if-else’ statement simple and understandable, nested if else statement is used only in If condition in below flow chart.

Nested if-else Statements

Try to understand Nested if-else statement using below given program : 

  • first validating age condition, if true print message “Person age wise eligible for blood donation” and further validate next condition
    1. Validate gender condition, if  satisfied as ‘Female’ print message  “Donar is Female, Checking for weight!” and further validate weight condition
      1. Validating weight if greater than 45 print “This female member is eligible to donate blood”
      2. Else print message “Not Eligible for blood donation, required criteria for female member:  age is greater than 18 and weight is greater than 45.”
    2. Otherwise if the gender is male,  in ‘else’ condition print message “Donar is male, Checking for weight!” and further validate weight condition
      1. Validating weight if greater than 55 print “This male member is eligible to donate blood”
      2. Else print message “Not Eligible for blood donation, required criteria for female member:  age is greater than 18 and weight is greater than 55.”
  • Otherwise if age is less than 18 print message “Age must be greater than 18 for blood donation”

So the Output of this program is :

Person age wise eligible for blood donation

Donar is male, Checking for weight!

Not Eligible for blood donation, required criteria for male member:  age is greater than 18 and weight is greater than 55.

int age = 22;
		int weight = 20;
		String gender = "M";
		if (age >= 18) {
			System.out.println("Person age wise eligible for blood donation");
			if (Gender.FEMALE.equals(gender)) {
				System.out.println("Donar is Female, Checking for weight!");
				if (weight > 45) {
					System.out.println("This female member is eligible to donate blood");
				} else {
					System.out.println(
							"Not Eligible for blood donation, required criteria for female member:  age is greater than 18 and weight is greater than 45.");
				}
			} else {
				System.out.println("Donar is male, Checking for weight!");
				if (weight > 55) {
					System.out.println("This male member is eligible to donate blood.");
				} else {
					System.out.println(
							"Not Eligible for blood donation, required criteria for male member:  age is greater than 18 and weight is greater than 55.");
				}
			}
		} else {
			System.out.println("Age must be greater than 18 for blood donation");
		}

Conclusion

Conditional statements are requisite tools in programming. It helps you to create flexible, decision-making code. From simple if statements to complex nested conditions, Java provides a range of options to cater different scenarios. By applying these conditional structures, you can enhance your programming skills and create more sophisticated and responsive applications.

P.S. We welcome your feedback. Please comment, if you find anything incorrect, or want to share more information about the topic or want to share any kind of feedback.

Leave a Reply

Your email address will not be published. Required fields are marked *