- If, If..else Statement in Java with Examples
- If statement
- Example of if statement
- Nested if statement in Java
- Example of Nested if statement
- If else statement in Java
- Example of if-else statement
- if-else-if Statement
- Example of if-else-if
- Top Related Articles:
- About the Author
- What is Nested if Statement in Java?
- Syntax of Nested If Statement
- Nested If Condition
- Flowchart
- Working
- Examples
- Implement Nested-If Condition With Only If Conditions
- Nested-If Condition With Both If and Else Conditions
- The Nested-If Condition That Takes Input From the User
- Conclusion
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:
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:
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"); >> >
Top Related Articles:
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.
What is Nested if Statement in Java?
Nested if refers to an if statement within an if statement. When we write an inner if condition within an outer if condition, then it is referred to as a nested if statement in java.
Nested if is a decision-making statement that works similar to other decision-making statements such as if, else, if..else, etc. It executes a block of code if the condition written within the if statement is true. However, in the nested-if statement, the block of code is placed inside another if block. And the inner block of code will execute only when the outer condition holds true.
Therefore, we use the nested if statement to evaluate more than one condition in a program and return multiple values depending on the test condition.
An example of a nested if statement could be a person should be qualified as well as an adult to be hired in a company. Therefore, in this example, first, the condition for the person’s qualification is checked, and if it is true, then only the company checks if the person is an adult or not. So, there are two conditions checked, one within another. Therefore they are nested if statements.
Syntax of Nested If Statement
The syntax for the nested if statement is as follows —
As you can see in the syntax, the inner if statement is only checked if the outer if condition is satisfied; otherwise not.
Nested If Condition
As discussed earlier, nesting refers to within. Nested if refers to if within an if statement. However, there can be any number of if statements within another if statement. The nested if statement comes under the category of the decision-making statements in Java.
Let us see the syntax for the infinite number of if statements, one within another.
However, if the outer if condition is satisfied and returns true, then only the inner if condition is checked; otherwise not. Moreover, you can also write an if..else statement within an if condition, which would be referred to as a nested if..else statement in Java.
Flowchart
Let us now understand the nested if with the help of a flowchart.
The flowchart has been created based on the below syntax —
The figure below depicts how the control in a nested if condition will flow when executed.
Working
The nested if statement works similar to an if..else statement. The only difference is that instead of an else statement, there would be another if statement in the syntax.
We will now see a code based on which the working of the nested if statement is explained further.
Let us understand how this nested if condition works when it is executed.
- If the test_condition1, that is, a == 97 is true, then go the test_condition2, which is z == 122 .
- If the test_condition2 is true, then execute the block of statement written inside the second if statement, which will print «Correct» as the output.
- However, if the test_condition2 is false, then the else part is executed if present in the program, otherwise, it will exit the loop. Here, in the above code, it is present, therefore, «Wrong» will be printed as the output.
- If the test_condition1 is false, then the corresponding outer else part is executed, if present, otherwise it will also exit the loop.
Examples
Implement Nested-If Condition With Only If Conditions
Now we will see an example where the nested if conditions are implemented only using the if condition.
In this example, we are given two numbers, x and y . These nummbers are to be checked if they are equal with 30 and 40 respectively based on which certain statement is printed.
Let us see the code for the same.
As you can see in the above code, first, the condition x == 30 is checked, and if satisfied, checks for the next condition y == 50 , which when satisfied, prints the subsequent statements in the if block.
Nested-If Condition With Both If and Else Conditions
Now let us see the nested if condition using both the if and else conditions in a program.
In this example as well, we are given two numbers, x and y . These numbers are to be checked if they are equal with 30 and 40 respectively based on which certain statement is printed. However, the difference lies with the introduction of an else block in the program.
Let us see the code for the same.
As you can see in the above code, first, the condition x== 30 is checked. If this condition is true, then only the nested if..else condition is checked further. However, if the first condition is true then the next if condition y == 40 is checked. If it is true, then the if block is executed; otherwise, the statements of the else block are executed.
The Nested-If Condition That Takes Input From the User
Now let us see the nested if conditions that take the input from the user.
In this example, we will find the right vaccine to be injected into a particular person. The conditions for getting a vaccine are that a person must be above 18 years of age, after which different genders will be provided with different kinds of vaccines in the market.
In our problem, we will be taking the age and the gender of a person as the input from the user.
Let us see the code for the same.
The output «CovidShield» came when the gender and age variables are provided with the input ‘M’ and 30 .
Therefore, as you can see in the above code, first of all, the inputs are taken from the user, after which if the person is above 18 years old then only they are provided with a different type of vaccine according to their gender.
First of all, if the gender is ‘M’, that is, a male, then they are provided with «CovidShield vaccine». However, if the gender is ‘F’, that is, female, they are injected with «Covaxine» whereas the rest all the genders have been provided with the «Sputnik» dose.
Conclusion
- Nested if statement is a decision-making statement that is used when we have to make decisions based on certain conditions.
- The nested if statement in Java is a set of if conditions one within another.
- The inner if conditions are only executed when the outer if condition results in true ; otherwise the corresponding else block is executed.
- The nested if condition can also be used with nested if..else statement.