If else in java

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

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

This statement executes if condition is true.

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

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.

if(condition)
{
   // Statements;
   if(condition)
   {
      // Statements;
   }
}

if else in java

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 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

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

if else statement

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,

       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.

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”. This statement is used when we need to check multiple conditions.

Also read – iterate over array using foreach loop in java

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 in java

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


Related Posts