If else in java

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

In java, decision making is made through control flow statements based on a condition. Below are four types of control statements, they are,

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

If else statement

Here if statements evaluate to true “if” block is executed else “else” block is executed. If else statement provides secondary path of execution.

Also read – do while loop java

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

If else in java

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.


Nested if statement

Nested if statement means one under the other. That is you can write one if statement inside another if statement.

Also read – java for loop example

Please note : Inner if statement executes only if outer if and inner 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 if…. else statement

If-else-if-else statement is used when we need to check multiple conditions. Here “if” statement can have 0 or more than one “else if” statements.

Also read – iterate over array using foreach loop in java

All these “else if” statements should be written between “if and else” statement. It’s also known as “If-else-if-else ladder” statement.

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

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


If statement

If statement executes a set of statements 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!!

Related Posts