If Else in Java

Java If-else Statement:

What is the if-else statement in Java?

If else statement is a condition statement that is used in the execution of a computer program in pre-defined rules. The if-else statement helps you to run a specific block of a program if the condition is true or else, it will check other conditions. It is used to control the flow or to determine the rules in a program.

Mentioned below are the four types of ‘if else’ statements in the Java programming language.

  • If statement
  • If-else statement
  • If-else-if ladder
  • Nested if statement

If statement:

The ‘If statement’ is a rule that is performed when the condition is satisfied or true.

Syntax:

If (condition){

//code to be executed

}

Code 1:

public static void main(String[] arg){

// here we declare variable

int age = 20; //integer data format

// here we check the age is greater than 18

if(age>18)

{

System.out.println("Age is greater than 18");// print output

}

}

Output:

Age is greater than 18.

Screenshot:

JavaIf-elsecode1

JavaIf-elsecode1

Full Stack Java Developer Course

The Gateway to Master Web DevelopmentExplore Course
Full Stack Java Developer Course

If with String:

The if string rule is used to compare two constant dates which cannot be changed, and it will give the result if the condition is true. 

The example for the if string statement code is given below.

Code 2:

public static void main(String[] arg){

// here we declare variable

String location = "paris"; //String(Collection of Character) data format

// here we check the the location is paris

if(location.equals("paris")) // here we use equals method its inbuilt method

{

System.out.println("Location is Same");// print output

}

}

Output:

Location is the same.

Screenshot:

JavaIf-elsecode2
JavaIf-elsecode2

Code 3:

public static void main(String[] arg){

// here we declare variable

String location = "paris"; // String(Collection of Character) data format

// here we check the location is paris or not

if (location.equals("london")) // here we use equals method its inbuilt method

{

System.out.println("location is same");// print output

} else {

System.out.println("location is not same");// print output

}

}

Output:

Location is not the same.

Screenshot:

JavaIf-elsecode3

JavaIf-elsecode3

Java if-else statement: 

In Java, the if-else condition is a set of rules or statements that perform a distinct set of conditions. Depending on rules, it will display true if the condition is satisfied, and the output will be false if the condition is not true.

Syntax:

If(condition){

// code if condition is true

}else{

//code if condition is false

}

Code 4:

public static void main(String[] arg){

// here we declare variable

int checkingNumber=13;  

    //Check if the number is divisible by 2 or not  

    if(checkingNumber%2==0){  

        System.out.println(checkingNumber+" is even number");  

    }else{  

        System.out.println(checkingNumber+" is odd number");  

    }  

}

Output:

13 is an odd number.

Screenshot:

JavaIf-els4ecode

JavaIf-elsecode4

Java if-else-if Ladder Statement:

The if else ladder statement is a set of conditions which is based on “Nested if condition”. The if-else ladder condition performs the expressions from top to down. Once the condition is true, the statement for the same is executed, and the balance ladder rules are skipped.

Syntax:

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 condition is false

}

Code 5:

public static void main(String[] arg){

//here we declare a variable  

  int internalMarks=65;  

      // here we checking the mark with if and else if condition

  // if we want check more than one condition we will use this if and else if

    if(internalMarks<50) // checks if condition is true

    {  

        System.out.println("fail");  

    }  

    else if(internalMarks>=50 && internalMarks<60) //checks if previous condition is false

    {  

        System.out.println("C grade");  

    }  

    else if(internalMarks>=60 && internalMarks<70) //checks if previous condition is false

    {  

        System.out.println("B grade");  

    }  

    else //checks if all the condition is false

    {

    System.out.println("A grade"); 

    }

}

Output: 

B grade.

Screenshot:

JavaIf-elsecode5

JavaIf-elsecode5

FREE Java Certification Training

Learn A-Z of Java like never beforeEnroll Now
FREE Java Certification Training

Java Ternary Operator if-else Statement:

The ternary operator is a condition that has three arguments,the first is to determine the comparison, the second one is to determine the result on true comparison & the third is vice versa of the second, which will give the result by comparing the false statement. If the conditions help in a shorter way of writing if else statement. 

For example, variable=Expression? Expression1: expression2

How does it work in the ternary operator?

  • If the expression is true, the expression is assigned to the variable
  • If the expression is false, the expression is assigned to the variable

Code 6:

public static void main(String[] arg){

//here we declare a variable  

int checkingNumber=13;    

    //Using ternary operator  

    String output=(checkingNumber%2==0)?"even number":"odd number";//like single line if and else statement  

    System.out.println(output);

    // ternary operator  ?: if the condition is true it will take before colon values 

   // if condition is false it will take after the colon values

}

Output:

Odd number.

Screenshot:

JavaIf-elsecode6

JavaIf-elsecode6

Java in Nested Ternary Operators if-else statement:

It is the nested ternary operator in Java. Here, it is possible to use one ternary operator inside another ternary operator.

Java Nested if Statement:

In theJava nested if statement, it is also possible to use the if-else statements inside an if-else statement.

Syntax:

If(condition){

//code to be executed

If(condition){

//code to executed

}

}

Code 7:

public static void main(String[] arg) {

// Creating two variables for age and weight

int personage = 25;

int personweight = 48;

// applying condition on age and weight

if (personage >= 18)

{

if (personweight > 50) //here we check the person weight is greater than 50

{

System.out.println("You are eligible to donate blood");

}

 else

{

System.out.println("You are not eligible to donate blood");

}

else 

{

System.out.println("Age must be greater than 18");// if age is less than 18 this condition will execute

}

}

Output: 

You are not eligible to donate blood.

Screenshot:

JavaIf-elsecode7

 JavaIf-elsecode7

Get a firm foundation in Java, the most commonly used programming language in software development with the Java Certification Training Course.

Conclusion:

To acquire expertise in Java, please enter our Full Stack Java Developer Job Guarantee Program, which is a robust six-month program in collaboration with HackerEarth and HIRIST. It has 250+ hours of Applied Learning designed to help you advance your career as a Full Stack Developer, by concentrating on industry demand and paying up to 5 LPA as a starting salary. 

This Full Stack Java Developer course will teach you the fundamentals of front-end, middleware, and back-end Java web development. You'll learn how to create an end-to-end framework, test and deploy code, and use MongoDB to store data, among other things.

Have any questions for us? Leave them in the comments section of this article, and our experts will get back to you on them, as soon as possible!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.