If else java statements

  • Post author:
  • Post category:java-category
  • Reading time:6 mins read

If else java statements

In this article, we’ll learn about the if-else java. Two selection statements used in Java: one is if and second is a switch. These two statements (if, switch) used to control the flow of the source program’s execution based upon conditions. The if statement belongs to the Java conditional branch statement. It used to route the source program execution through two different paths. The general form of the if statement shown below:

   if (conditions)
   {
	if statements;
   }
   else
   {
	else statements;
   } 

If else java

The body of the if statement can have one or further statements which enclosed with curly braces. The portion of if the condition can have any regular expression that returns a boolean value. Keyword else is the optional which used, when if condition returns false state. When the condition of if true then execute if body statements otherwise execute else body statements.

Flowchart of if:

if else flowchart

Example program:

   int a = 5;
   int b = 7;
   if (a>b)
   {
	System.out.println(" Value of "a" is greater: ");
   }
   else
   {
	System.out.println(" Value of "b" is greater: ");
   }

Output:

Value of “b” is greater:

Here b is greater than a. Therefore execute the body of else which contains only one statement “Value of “b” is greater: ”. If variable a contain value 7 and b contain value 5 then execute the body of if and print on the screen “Value of “a” is greater: ”.

Note: After the if or the else statement only one statement can appear directly. If you want to include more statements, you’ll need a block for multiple statements.

Example single statement:

   if (a>b)
   System.out.println(“a is greater: ”);
   Else
   System.out.println(“b is greater”);

Example multiple statements:

   if (a>b)
   {
	System.out.println(“a is greater: ”);
	System.out.println(“b is smaller: ”);
   }
   Else
   {
	System.out.println(“b is greater: ”);
	System.out.println(“a is smaller: ”);
   }

Nested ifs

A nested if in Java programming is also simple if statement that has another if statements in your body. In Java programming nested ifs techniques are commonly used. Nested ifs work the same as simple if statement. This technique is used when the programmer has multiple conditions within a single if statement.

Flowchart of if-else:

nested if else

The general form of Nested ifs:

   if (conditions)
   {
      if (conditions)
      {
	 if (conditions)
         {
	    Statements;
         }
      }
   }
   else
   {
     Statements;
   }

Example program:

   if (x > 50)
   {
      if (x < 100)
      {
	 if (x == 75)
         {
	    System.out.println(“Value of x is 75: ”);
         }
      }	
   }
   else
   {
      System.out.println(“Value of x is smaller than 50: ”);
   }

The if-else-if

The if-else-if is also a selection statement which used when a programmer has multiple conditions according to a single if portion and also else portion have further in again if portion and so on.

The general syntax of if-else-if shown below:

   if (conditions)
   {
      Statements;
   }
   else if (conditions)
   {
      Statements;
   }
   else if (conditions)
   {
      Statements;
   }
   else
   {
      Statements;
   }

Flowchart of if-else-if:

if else if

Below program use the if-else-if:

   class ifElseIfDemo 
   {
     public static void main(String args[])
     {
       int mon = 4; // April
       String season11;
       if(mon == 12 || mon == 1 || mon == 2)
          season11 = "Winter season:";
       else if(mon == 3 || mon == 4 || mon == 5)
          season11 = "Spring season:";
       else if(mon == 6 || mon == 7 || mon == 8)
          season11 = "Summer season:";
       else if(mon == 9 || mon == 10 || mon == 11)
          season11 = "Autumn season:";
       else
          season11 = "Wrong Month";
       System.out.println("Month april is in the " + season11 + ".");
       }
    }

Output:

Month April is in the Spring season:

Operator in Java

admin

We are a team of writers, researchers, and editors who are passionate about helping others live their best lives. We believe that life is a beautiful gift. We try to live our lives to the fullest and enjoy every moment. We are always learning and growing, and we cherish the relationships we have with our family and friends.

Leave a Reply