Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

How To Implement If Else In Java?

Published on Jul 31,2019 4.3K Views


Conditions in Java can be tested by using the if statement. The if statement can be followed by an else statement as well, which is executed when the Boolean expression is false. This article will discuss If Else Statement In Java.

Following pointers will be covered in this article,

Starting Off with this article on if else in JAVA.

There are multiple types of if statements in java:

if Statement

The if statement is used to test the condition and is followed by a set of statements. The statements execute only when the condition proves to be true.

Syntax:

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

Example

public class Test 
{  
public static void main(String[] args) 
{  
    //defining a 'price' variable  
    int price=1800;  
    //checking the price  
    if(price>1500){  
        System.out.print("Price is greater than 1500");  
    }  
}  
}

Output:

Price is greater than 1500

Moving on with this article on if else in JAVA.

if-else statement

The if-else statement in java is also used for testing conditions. The if block is executed if the condition is true. If the condition is false, the else block is executed.

Syntax:

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

Example:

public class Test 
{  
public static void main(String[] args) 
{  
    //defining a variable  
    int num=15;  
    //Checking if the number is divisible by 2 
    if(num%2==0){  
        System.out.println("Even number");  
}else
{  
        System.out.println("Odd number");  
 }  
}  
}  

Output:

Odd number

Let’s take a look at another example, wherein the program checks if the year entered is a leap year or not.

Example:

public class Test {    
public static void main(String[] args) {    
    int year=2028;    
    if(((year % 4 ==0) && (year % 100 !=0)) || (year % 400==0)){  
        System.out.println("LEAP YEAR");  
    }  
    else{  
        System.out.println("NOT A LEAP YEAR");  
    }  
}    
} 

Output:

LEAP YEAR

Moving on with this article on if else in JAVA.

Using Ternary Operators

Ternary operators (? : ) can be used instead of the if else statement. If the condition appears to be true, the result of ? is returned. If it is false, the result of : is returned.

Example:

 public class Test {    
public static void main(String[] args) {    
    int num=12;    
    //Using ternary operator  
    String output=(num%2==0)?"Even number":"Odd number";    
    System.out.println(output);  
}    
}     

Output:

Even number

Moving on with this article on if else in JAVA.

if-else-if ladder :

One block of code can be executed among multiple blocks, using the if-else-if ladder.

Execution of these statements take place from the top.

When the test expression appears to be true, the code present in the body of the if statement is executed. If none of the test expressions are true, the else statement is executed.

Example:

 public class Test {
   public static void main(String[] args) {   
      int num = 15;
	 
      if (num > 0) {
         System.out.println("POSITIVE NUMBER");
      }
      else if (num < 0) {
         System.out.println("NEGATIVE NUMBER");
      }
      else {
         System.out.println("NUMBER 0");
      } 
   }
}

Output

POSITIVE NUMBER

Moving on with this article on if else in JAVA.

Nested if statement:

This statement is represented by an if block with another if block. For the inner if block to execute, the condition of the outer block should be true.

Syntax:

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

Example:

 public class Test {      
public static void main(String[] args) {      
    //Creating two variables    
    int age=20;    
    int weight=55;      
    //applying conditions
    if(age>=18){      
        if(weight>50){    
            System.out.println("You are allowed to trek.");    
        } else{  
            System.out.println("You are not allowed to trek.");    
        }  
    } else{  
      System.out.println("Must be above the age of 18.");  
    }  
}  }

Output:

You are allowed to trek. 

The if-else statement in java allows the user to test innumerous conditions in an extremely efficient manner.

Thus we have come to an end of this article on ‘if else in Java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How To Implement If Else In Java?

edureka.co