Java if-else Statements

If else statements in Java is also used to control the program flow based on some condition, only the difference is: it's used to execute some statement code block if the expression is evaluated to true, otherwise executes else statement code block.

The basic format of if else statement is:

   //execute your code
   //execute your code

Figure - Flowchart of if-else Statement:


Example of a Java Program to Demonstrate If else statements

public class Sample {

 public static void main(String args[]) {
  int a = 80, b = 30;

  if (b & gt; a) {
   System.out.println("b is greater");
  } else {
   System.out.println("a is greater");
Program Output:


Here are few other related articles for you to read:

Scroll Back to Top