Java if-else Statements

thumbnail

If else statements in Java used to control flow of the program based on certain condition we applied. The only difference is: If-else statements is used to execute some statement code block if the expression is evaluated to be true, otherwise it will executes else statement code block.

The basic format of if-else statement is:

if(test_expression)
{
   //execute your code
}
else
{
   //execute your code
}

Example of a Java program to show the If-else statements

Example 1:

public class SampleIfElse {

 public static void main(String args[]) {
  int valueA = 90, valueB = 20;

  if (valueB  > valueA ) {
   System.out.println("valueB  is greater");
  } else {
   System.out.println("valueA  is greater");
  }
 }
}

Example 2:

public class SampleIfElse {

 public static void main(String args[]) {
  int valueA = 20, valueB = 90;

  if (valueB  > valueA ) {
   System.out.println("valueB  is greater");
  } else {
   System.out.println("valueA  is greater");
  }
 }
}
Back To Top