If Else Java

Hey guys!! Welcome to flower brackets blog. In today’s post you will learn if else java statement.

Below are different types of if statements, they are,

  1. if else statement
  2. nested if statement
  3. if else if ladder
  4. if statement.

If statement

If statement executes if condition is true.

Syntax :

if(condition)
{
   // if condition is true;
}

Example:

public class IfStatement
{
   public static void main(String[] args)
   {
      int empAge = 50;
      if(empAge > 41)
      {
         System.out.println("Employee age is greater than 41!!");
      }      
   }
}

Output :

Employee age is greater than 41!!


Nested if statement

Nested if statement means one under the other, meaning, you can write one if or else if statement inside another if or else if statement.

Also Read – Java For Loop Example

Please note : Inner if statement is executed only if outer if condition is true.

Syntax :

if(condition)
{
   // Statements;

   if(condition)
   {
      // Statements;
   }
}

Nested if

Example:

public class NestedIfExample
{
   public static void main(String[] args)
   {
      int a = 50;
      int b = 20;
      if(a == 50)
      { 
         if(b == 20)
         {
            System.out.println("Both a and b values are equal");
         }
      }
   }
}

Output :

Both a and b values are equal


If else statement

If else statement in java provides secondary path of execution. If block is executed if condition is true else the else block is executed.

Also Read – Do While Loop Java

Syntax:

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

if else statement

Example:

import java.util.Scanner;

public class JavaIfElse
{
   public static void main(String[] args)
   {
      int marksObtained;
      int marksPass;

      marksPass = 40;

      Scanner sc = new Scanner(System.in);
      System.out.println("Enter marks obtained by James : ");
      marksObtained = sc.nextInt();

      if(marksObtained >= marksPass)
      {
         System.out.println("James has passed the test.");
      }
      else
      {
         System.out.println("James has failed the test.");
      }
   }
}

Output:

Enter marks obtained by James : 80
James has passed the test.


Java ternary operator

Java ternary operator is a conditional operator which takes three operands. Java ternary operator can be used in the place of if-else statement and switch case.

Here’s the syntax of ternary operator in java and example,

Syntax :

       True
 _______________
|               |
Condition ? Block 1 : Block 2
|_______________________|
          False

1. Ternary operator uses three operands.
2. Java ternary operator is used as shorthand for if-else statement.
3. Here first operand should be boolean or statement with boolean result.

Example:

public class JavaTernary
{
   public static void main(String[] args)
   {
      int number1 = 50;
      int number2 = 100;
      int output;
 
      boolean temp = true;

      output = temp ? number1 : number2;
 
      System.out.println("temp is true - " + output);
 
      temp = false;
 
      output = temp ? number1 : number2;
 
      System.out.println("temp is false - " + output);
   }
}

Output :

temp is true – 50
temp is false – 100


If else if ladder statement

Java if-else-if ladder statement is also known as “if else if ladder”. Java else if statement is used when we need to check multiple conditions.

Also Read – Iterate Over Array Using Foreach Loop In Java

Syntax :

if(condition1)
{
   // if condition1 is true;
}
else if(condition2)
{
   // if condition2 is true;
}
else if(condition3)
{
   // if condition3 is true;
}
.
.
.
else
{
   // if none of the above condition is false;
}

if else if ladder

Example:

public class IfElseIfLadderExample
{
   public static void main(String[] args)
   {
      int marks = 80;
      if(marks > 69)
      {
         System.out.println("First class");
      }
      else if(marks > 59)
      {
         System.out.println("Second class");
      }
      else if(marks > 49)
      {
         System.out.println("Third class");
      }
      else
      {
         System.out.println("Fail");
      }
   }
}

Output :

First class


Conclusion

I hope you have understood the concept. You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

Related Posts