If Else Java

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

We will also look into its definition, syntax, flow diagram and example. So let’s get into it,

If statement

The java if statement is something that is known as a control structure. This is something that is very common amongst all programming languages.

Also Read – Learn While Loop In Java

It’s not specific to the java programming language but the specific syntax that goes along with the if statement is specific to the java language.

So, let’s dive in and learn java if statement

Syntax :

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

Example :

public class IfStatement
{
   public static void main(String[] args)
   {
      int empAge = 50;
      if(empAge > 41)
      {
         System.out.println("Inside IF statement!!");
      }
      System.out.println("Outside IF statement!!");
   }
}

Output :

Inside IF statement!!
Outside IF statement!!


Nested if java

Nested if java is also known as “multiple if statements” in java. Here in nested if java there will be if statement inside another if statement.

Also Read – Java For Loop Example

Syntax :

if(condition1)
{
   // Statement1;

   if(condition2)
   {
      // Statement2;
   }
}

Note :

  1. In nested if java, statement1 will execute if condition1 is true.
  2. Statement2 will only execute if both condition1 and condition2 are true.

Example :

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

Output :

Employee age is less than 100!!
Employee age is greater than 50!!!!


If Else Java

If Else Java statement provides secondary path of execution. When an if block statement evaluates to false then else block statements are executed.

Also Read – Do While Loop Java

Syntax:

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

Flow Diagram :

If Else Java

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.


Ternary operator java

Now let’s learn how to use ternary operator in java.

Syntax :

Here’s the syntax of ternary operator in java

       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.

Example :

We will see ternary operator example now,

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


Else if statement java

Else if statement java 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)
{
   // execute if condition1 is true;
}
else if(condition2)
{
   // execute if condition1 is false;
}
else if(condition3)
{
   // execute if condition1 and condition2 are false;
}
.
.
.
else
{
   // execute if none of the condition is true;
}

Note :

  1. We can have multiple “else if”.
  2. In java else if statement, if the condition is true then the corresponding set of statements get executed, rest gets ignored.
  3. If none of the condition is true then the statements inside “else” gets executed.

Example :

Now let’s learn else if statement in java with an example

public class ElseIfStatement
{
   public static void main(String[] args)
   {
      int empAge = 45;
      if(empAge > 49)
      {
         System.out.println("Inside IF statement!!");
      }
      else if(empAge > 10)
      {
         System.out.println("Inside Else IF statement!!");
      }
      else
      {
         System.out.println("Inside Else statement!!");
      }
      System.out.println("Outside IF/Else statement!!");
   }
}

Output :

Inside Else IF statement!!
Outside IF/Else statement!!


conclusion

That’s it guys. This is all about if else java with example. 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.

You May Also Like