If Else Statement Java
Table of Contents
If Else Statement Java
If Else Statement Java
The If statements are used in Java to test the conditions. If the condition matches the assertion, the result is true; otherwise, the result is false. The four different types of if statements are as follows:
For instance, if we want to write a programme to test positive integers, we must first determine whether the integer is greater than zero. If statement is useful in this scenario.
There are four types of if statement in Java:
- if statement
- if-else statement
- if-else-if ladder
- nested if statement
if Statement
A single conditional statement, the if statement only runs if the given condition is true.
If Statement Syntax:
if(condition)
{
//code
}
So Using the diagram below, we can comprehend the if statement’s flow. It demonstrates that any code contained within the if statement will only run if the predicate is true.
Data-flow-diagram of If Block
Example:
In this example, we are testing students marks. If the marks are greater than 65 then student will get first division.
public class IfDemo1 {
public static void main(String[] args)
{
int marks=70;
if(marks > 65)
{
System.out.print("First division");
}
}
}
if-else Statement
If-else statements are employed to test conditions. So If the condition is met, the if block is executed; if not, the else block is. When we want to carry out an operation based on a false result, it is helpful. Only if the condition is false will the else block be executed.
Syntax:
if(condition)
{
//code for true
}
else
{
//code for false
}
In this block diagram, we can see that when condition is true, if block executes otherwise else block executes.
Data-flow-diagram of If Else Block
if else Example:
In this example, we are testing student marks, if marks is greater than 65 then if block executes otherwise else block executes.
public class IfElseDemo1 {
public static void main(String[] args)
{
int marks=50;
if(marks > 65)
{
System.out.print("First division");
}
else
{
System.out.print("Second division");
}
}
}
if-else-if ladder Statement
The if-else-if ladder statement is used in Java to test various scenarios. So It is employed to test a single condition across several statements. It is advised to use an if-else-if ladder when there are multiple conditions to be satisfied.
Syntax:
if(condition1)
{
//code for if condition1 is true
}
else if(condition2)
{
//code for if condition2 is true
}
else if(condition3)
{
//code for if condition3 is true
}
...
else
{
//code for all the false conditions
}
It contains multiple conditions and execute if any condition is true otherwise executes else block.
Data-flow-diagram of If Else If Block
Example:
Here, we are testing student marks and displaying result based on the obtained marks. If marks are greater than 50 student gets his grades.
public class IfElseIfDemo1 {
public static void main(String[] args) {
int marks=75;
if(marks<50){
System.out.println("fail");
}
else if(marks>=50 && marks<60){
System.out.println("D grade");
}
else if(marks>=60 && marks<70){
System.out.println("C grade");
}
else if(marks>=70 && marks<80){
System.out.println("B grade");
}
else if(marks>=80 && marks<90){
System.out.println("A grade");
}else if(marks>=90 && marks<100){
System.out.println("A+ grade");
}else{
System.out.println("Invalid!");
}
}
}
Nested if statement
The Nested If statement in Java consists of an if inside another if. When the outer block is true, only the inner block is executed in this case, where one if block is created inside another if block.
Syntax:
if(condition)
{
//statement
if(condition)
{
//statement
}
}
Data-flow-diagram of Nested If Block
Example:
public class NestedIfDemo1 {
public static void main(String[] args)
{
int age=25;
int weight=70;
if(age>=18)
{
if(weight>50)
{
System.out.println("You are eligible");
}
}
}
}