Nested if conditions in java

Java if. else if condition and nested if else

Sometime as a programmer you want to use a set of conditions where in you want that if your first condition is false then only move to check the next condition, and if that is also false then only move to check the next condition and so on. To address such scenarios java provides if then else if statement. You can specify another condition with else if statement which is evaluated only when it’s prior if or else if condition is false .

If the else if condition returns true , the else if block will be executed, if it returns false , computer will move to next else if statement and evaluates the condition expression. This process goes on until any of the expression returns true or all the else if statements are finished. Once any of the expression returns true , code inside that conditional statement is executed and all other condition statements after that are not executed. If all conditional statements returns false, then final else block(if any) will be executed. The syntax of if -then- else if statement is:

if(condition) < // code to be executed inside if block > else if(condition-1) < // code to be executed inside this else if block > else if(condition-2) < // code to be executed inside this else if block > . . . else if(condition-N) < // code to be executed inside this else if block > else < // code to be executed inside else block, it's optional >

The last else statement in if else-if ladder is optional, it’s programmer’s choice whether to use or not.

What is if-else if ladder statement in Java ?

The if .. else if statement given above is also called as else if ladder which is mostly used when programmer wants to select one block of code among many. In if .. else if ladder only one block(the one whose condition is true ) of code is executed.

Can I use else if statement without if statement ?

No, you can not use else if statement without if statement in java. The else if must be used along with if statement.

Flowchart of if. else if statement

if-else if statement in java

if .. else if program in Java

 public class IfElseIfStatement < public static void main(String [] args) < int num1 = 20, num2 = 20; if(num1 > num2) < System.out.println("num1 is greater than num2"); > else if(num1 == num2) < System.out.println("num1 is equal to num2"); > else < System.out.println("num1 is less than num2"); > System.out.println("code to be executed after else if ladder"); > >

num1 is equal to num2
code to be executed after else if ladder

Can we use if inside else if in Java ?

Yes, we can use if statement inside else if statement, the program will run successfully.

Nested if-else statement in Java

Java allows programmer to place if else block inside another if or else block. This is called as nested if else statement. Programmer can do any level of nesting in program which means you can place if else block inside another if or else block up to any number of times. Inner if block is executed only when outer if condition is true.

if(condition-1) < // code to be executed inside first if block if(condition-2) < // code to be executed inside second if block if(condition-3) < // code to be executed inside third if block . . . . . . . . . . > else < // code to be executed inside else block > // code to be executed inside second if block > // code to be executed inside first if block > else < if(condition-4) < // code to be executed inside this if block > >

Nested if else program in Java

 public class NestedIfElseStatement < public static void main(String [] args) < int num1 = 20; if(num1 >= 10) < if(num1 < 100) < System.out.println("num1 is a double digit number"); > else < System.out.println("num1 is not a double digit number"); > > else < System.out.println("num1 is less than 10"); > System.out.println("code after nested if else block"); > >

num1 is a double digit number
code after nested if else block

Читайте также:  Php что такое fallback

Can we use if inside if in Java ?

Yes, this is known as nested if in java. Refer the example above.

Can we use if inside else in Java ?

Yes, this is also known as nested if else in java. Refer the example above.

  • Ensure there is space between else & if, it should not be elseif.
  • The else if is also part of conditional or decision making statements.
  • If no curly brackets <> is used with else if keyword, only first line of code after else if keyword will be considered as part of else if statement.

Источник

Nested if Statements in Java

Nested if Statements in Java

Nested if Statement is one of the decisions making statements in Java that flows according to certain conditions. The branching of these conditions is a result of the program’s state change. That is, there will be an if-else condition inside another if-else. If, if-else, if-else-if, jump, switch-case, etc., are some of the other decision making statements in Java. Now, let us see the Nested-if Statement in detail.

Syntax of Nested if Statement

Following is the syntax of Nested if Statement in Java.

Here, Cond1 is Condition 1, and Cond2 is Condition 2.

Flowchart

The following figure depicts the flow chart of the Nested-if condition.

Nested if Statements in Java

Working of Nested if Statements in Java

Nested-If works similar to the normal If-else condition. The only difference is that there will be an if condition inside another if condition. The working will be as follows.

  • If Condition 1 is True, then go to if condition 2. If condition 2 is satisfied, its body will execute; otherwise, else part will execute.
  • If Condition 1 is False, then the body of the else part will be executed.
  • Once the condition check is finished, exit the loop.
  • Continue the execution of statements after the loop

The count of the if-else condition varies depending on the user’s requirement.

Examples of Nested if Statements

In order to understand Nested-if in detail, let us see the examples using Java.

Example #1

A simple java program to implement Nested-if condition with only if conditions.

//Nested-if Java program with if conditions only public class NestedIfExample < public static void main(String args[]) < //declare 2 variables and store some values in it int num1 = 23; int num2 = 45; //if the number 1 is 23 if( num1 == 23 ) < //if number is 45 if( num2 == 45 ) < System.out.print("Number 1 is :"+ num1 +" and Number 2 is :"+ num2); >// end of if condition 2 > //end of if condition 1 > //end of main method > //end of class

Nested if Statements in Java output 1

In this program, two variables- num1 and num2 are declared that stores two numbers, 23 and 45, respectively. In the if condition, num1 is checked whether it is 23. As it is true, then nested if gets executed. That is, another if condition, whether number 2 is 45, is also checked. As it is also true, a line is printed, displaying “Number 1 is 23, and Number 2 is 45”.

Читайте также:  Cli php shop followup

Example #2

A simple java program to implement Nested-if condition with both if and else conditions.

//Nested-if Java program with both if and else conditions public class NestedIfExample < public static void main(String args[]) < //declare 2 variables and store some values in it int num1 = 23; int num2 = 48; //if the number 1 is 23 if( num1 == 23 ) < //if number is 45 if( num2 == 45 ) < System.out.print("Number 1 is :"+ num1 +" and Number 2 is :"+ num2); >// end of if condition 2 else < System.out.print("Number 2 is not 45"); >//end of else condition 2 > //end of if condition 1 > //end of main method > //end of class

Nested if Statements in Java output 2

In this program, two variables- num1 and num2 are declared that stores two numbers, 23 and 48, respectively. In the if condition, num1 is checked whether it is 23. As it is true, then nested if gets executed. That is, another if condition, whether number 2 is 45, is also checked. As it is not true, a line gets printed displaying “Number 2 is not 45”.

Example #3

A simple java program to implement a Nested-if condition that takes input from the user.

//Nested-if Java program that takes input from user and checks the condition import java.util.Scanner; public class NestedIfExample < public static void main(String args[]) < //create object of scanner Scanner sc= new Scanner(System.in); System.out.print("Enter the number to be checked: "); int num1 = sc.nextInt(); //if the number 1 is greater than or equal to 23 if( num1 >= 23 ) < System.out.print("Number 1 is :"+ num1 +" and it is greater than 23."); //if number is 45 if( num1 >= 45 ) < System.out.print("Oh!! it is greater than 45 also"); >// end of if condition 2 else < System.out.print(" But, the number "+num1+" is less than 45"); >//end of else condition 2 > //end of if condition 1 else < System.out.print("The number "+num1+" is less than 23"); >//end of else condition 2 > //end of main method > //end of class

output

In this program, the num1 variable is declared. Then, the user is asked to input num1. Here, 33 is given as input, and In the if condition, num1 is checked whether it is greater than or equal to 23. As it is true, then nested if it gets executed. That is, another if condition, whether number 2 is greater than or equal to 45, is also checked. As it is not true, a line is printed, displaying, “Number 1 is 33, and it is greater than 23. But the number 33 is less than 45”.

Suppose we have given input as 20. What will be the output?? Let us check how the flow will be in that case.

output

In this case, as the first condition itself is not satisfied, the else part gets executed. That is, a line will be printed as” The number 20 is less than 23”.

Conclusion

Nested if Statement is a decision-making statement in Java containing certain branches with an if condition inside another if condition. Syntax, working, and examples of Nested-if is discussed in this document.

This is a guide to Nested if Statements in Java. Here we discuss the Flowchart and Working of Nested if Statements in Java along with the examples and outputs. You may also have a look at the following articles to learn more –

89+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

97+ Hours of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

JAVA Course Bundle — 78 Courses in 1 | 15 Mock Tests
416+ Hours of HD Videos
78 Courses
15 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.8

Читайте также:  Какая кнопка нажата java

Источник

If, If..else Statement in Java with Examples

When we need to execute a set of statements based on a condition then we need to use control flow statements. For example, if a number is greater than zero then we want to print “Positive Number” but if it is less than zero then we want to print “Negative Number”. In this case we have two print statements in the program, but only one print statement executes at a time based on the input value. We will see how to write such type of conditions in the java program using control statements.

In this tutorial, we will see four types of control statements that you can use in java programs based on the requirement: In this tutorial we will cover following conditional statements:

a) if statement
b) nested if statement
c) if-else statement
d) if-else-if statement

If statement

If statement consists a condition, followed by statement or a set of statements as shown below:

if statement flow diagram

The statements gets executed only when the given condition is true. If the condition is false then the statements inside if statement body are completely ignored.

Example of if statement

public class IfStatementExample < public static void main(String args[])< int num=70; if( num < 100 )< /* This println statement will only execute, * if the above condition is true */ System.out.println("number is less than 100"); >> >

Nested if statement in Java

When there is an if statement inside another if statement then it is called the nested if statement.
The structure of nested if looks like this:

Statement1 would execute if the condition_1 is true. Statement2 would only execute if both the conditions( condition_1 and condition_2) are true.

Example of Nested if statement

public class NestedIfExample < public static void main(String args[])< int num=70; if( num < 100 )< System.out.println("number is less than 100"); if(num >50) < System.out.println("number is greater than 50"); >> > >
number is less than 100 number is greater than 50

If else statement in Java

This is how an if-else statement looks:

If else flow diagram

The statements inside “if” would execute if the condition is true, and the statements inside “else” would execute if the condition is false.

Example of if-else statement

public class IfElseExample < public static void main(String args[])< int num=120; if( num < 50 )< System.out.println("num is less than 50"); >else < System.out.println("num is greater than or equal 50"); >> >
num is greater than or equal 50

if-else-if Statement

if-else-if statement is used when we need to check multiple conditions. In this statement we have only one “if” and one “else”, however we can have multiple “else if”. It is also known as if else if ladder. This is how it looks:

if(condition_1) < /*if condition_1 is true execute this*/ statement(s); >else if(condition_2) < /* execute this if condition_1 is not met and * condition_2 is met */ statement(s); >else if(condition_3) < /* execute this if condition_1 & condition_2 are * not met and condition_3 is met */ statement(s); >. . . else < /* if none of the condition is true * then these statements gets executed */ statement(s); >

Note: The most important point to note here is that in if-else-if statement, as soon as the condition is met, the corresponding set of statements get executed, rest gets ignored. If none of the condition is met then the statements inside “else” gets executed.

Example of if-else-if

public class IfElseIfExample < public static void main(String args[])< int num=1234; if(num =1) < System.out.println("Its a two digit number"); >else if(num =100) < System.out.println("Its a three digit number"); >else if(num =1000) < System.out.println("Its a four digit number"); >else if(num =10000) < System.out.println("Its a five digit number"); >else < System.out.println("number is not between 1 & 99999"); >> >

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

Источник

Оцените статью