Home >>Java Tutorial >Java If else Statement

Java If else Statement

Java If-else Statement

The Java if statement is generally used to test the conditions that are provided by the user. It basically inspects the boolean condition that is true or false. The if statements in Java are of various types that are depicted below:

  • if statement
  • if-else statement
  • if-else-if ladder
  • nested if statement

1. if Statement in Java

Like all other statements in Java the if statement basically used to tests the given condition. This statement generally executes the if block if the condition is found to be true.

Syntax

Here is the syntax of the if statement in the Java language:

Syntax:

if(condition)
{  
//code to be executed  
}  

Here is an example of the Java if statements that will help you understand the concept very clearly:

public class DecisionMaking
{  
	public static void main(String[] args) 
	{  
    //defining a num variable  
    int num=10;  
    //checking the number is divisible by 2  
    if(num%2==0)
	{  
        System.out.print("Given number is even no");  
    }  
}  
}  

2. if-else Statement in Java

Just like the Java if statement, the if-else statement is also used to test the condition. And it generally executes the if block if the condition is found to be true, if the condition is not found to be true then the statement is not executed.

Syntax

Here is the syntax of the if-else statement in Java:

if(condition)
{  
//code if condition is true  
}
else
{  
//code if condition is false  
}

Here is an example of the java if-else statement that will clear your doubts about the working of it:

public class DecisionMaking 
{  
public static void main(String[] args) 
{  
    int num=10;  
    //Check whether the number is divisible by 2  
    if(num%2==0)
	{  
        System.out.println("Given number is even");  
    }
	else
	{  
        System.out.println("Given number is odd");  
    }  
}  
}  

Using Ternary Operator

In order to perform the task of if...else statement the users can also use the ternary operator (? :). This is used because it is known as a shorthand way to check the condition. If the condition is found to be true then the result of ? will be returned. And, if the condition is found to be false then the result of: will be returned.

Here is an example that will help you understand the use of ternary operator better:

public class DecisionMaking 
{    
public static void main(String[] args) 
{    
    int num=10;    
    //Check even and odd number with the help of  ternary operator  
    String result=(num%2==0)?"Given number is even":"Given number is odd";    
    System.out.println(result);  
}    
}  

3. Java if-else-if ladder Statement

The if-else-if ladder statement in java is known to execute only one condition from a set of multiple statements.

Syntax

Here is the syntax of the if-else-if ladder statement in java:

if(condition1)
{  
//code to be executed if condition1 is true  
}
else if(condition2)
{  
//code to be executed if condition2 is true  
}  
else if(condition3)
{  
//code to be executed if condition3 is true  
}  
...  
else
{  
//code to be executed if all the conditions are false  
}

Here is an example that will help you in understanding the if-else-if ladder statement:

//Java Program to demonstrate the use of If else-if ladder.  
//This program will print the day name  
public class DecisionMaking 
{  
	public static void main(String[] args) 
	{  
		int day=3;  
		  
		if(day==1)
		{  
			System.out.println("Its monday");  
		}  
		else if(day==2)
		{  
			System.out.println("Its tuesday");  
		}  
		else if(day==3)
		{  
			System.out.println("Its wednesday");  
		}  
		else
		{  
			System.out.println("Invalid choice");  
		}  
	}  
}  

Here is another example that will help you understand the program to check positive, negative or zero:

public class CheckPosNeg
{    
public static void main(String[] args) 
{    
    int num=10;    
    if(num>0)
	{  
    System.out.println("Its Positive number");  
    }
	else if(num<0)
	{  
    System.out.println("its Negative number");  
    }
	else
	{  
    System.out.println("Given number is zero");  
	}  
}    
}    

4. Java Nested if statement

The nested if statement in java generally represents the if block that is within another if block. The nested if statement generally works this way like the inner if block condition will be executed only if the outer if block condition is found to be true.

Syntax

Here is the syntax of the nested if statement in Java depicted below:

if(condition){    
     //code to be executed    
          if(condition){  
             //code to be executed    
    }    
}  

Here are the examples of the nested if statement in Java that will help you understand their concept very clearly:

public class NestedIfEx
{    
public static void main(String[] args) 
{    
    int x=10;  
    int y=15;    
    if(x>=10)
	{    
        if(y>=15)
		{  
            System.out.println("Welcome user");  
        }    
    }    
}
}  

No Sidebar ads