Java if else

In this tutorial, we will learn about if, if - else, and else-if ladder statements of java with syntax, flow diagram of each statement and an example of it.

Java if else

In java, if and else statements are used to control the execution flow of statements based on one or more conditions, we have the following control statements available.

  1. if condition
  2. if and else condition
  3. if, else if and else condition.

if condition

if condition prevents or executes its block based on the condition result associated with it.

Syntax:

if(condition) {
// block code executed when condition is true
}

Condition can be anything but the result should boolean value either true or false, multiple condition can be passed by using the logical operators.

A method can be passed as condition to the if statement but the return type of the method should be a boolean type.

Example:

Copied
public class IfExample { 
    public static void main(String[] args) { 
        int digit = 1000; 
        if (digit > 100) {             
	System.out.println("Given number is greater than 100");
        } 
    }
}

Output:

Given number is greater than 100

Above output gets printed after running the example, lets understand the execution control.

  1. Declared a digit variable with type Integer and value as 1000
  2. 1000 is greater than 100 condition is true.
  3. Statements under if block gets executed the print statement.

The following is the flow diagram of the if statement, when its true if block gets executed and false will skip its execution.

if flow diagram

If else condition

if else condition comes handy to execute a block of statements when the condition is not satisfied. Still in the same manner If allows to execute only when condition is satisfied and else block gets executed only when condition is not satisfied.

Syntax:

if(condition) {
// gets executed when condition is true
}
  else {
   // gets executed when condition is false
}

Example:

Copied
public class IfElseExample { 
    public static void main(String[] args) { 
        int digit = 10; 
        if (digit > 100) {             
	    System.out.println("Given number is greater than 100");
        } 
          else {
	     System.out.println("Given number is less than 100");
	   }
    }
}

Output:

Given number is less than 100

Above line gets printed after executing the example, lets understand how the else condition executed.

  1. Declared a digit variable with type Integer and value as 10
  2. 10 is not greater than 100 condition is not satisfied.
  3. If block statements not executed.
  4. Condition is not satisfied therefore else block statements are executed and prints the line.

The following is the flow diagram of the if else statement shows flow execution steps.

if else flow diagram

If .. else if and else condition

If and else if will allows to perform alternative conditions check and perform alternative actions. Consider we have a job to post grade of students based on marks.

  1. Student who scored above 90 will be under a grade of A+.
  2. Student who scored above 75 and below 90 will be under a grade of A.
  3. Student who scored above 40 and below 75 will be under a grade of A-.
  4. Student who scored below 40 will be no grade.

Considering above criteria, student grade will decide based on marks and it may vary from student to student in terms of marks scored.

Example:

Copied
public class IfElseIfExample {
	public static void main(String[] args) {
		int stdMarks = 76;
		if(stdMarks >= 90) {
			System.out.print("Student under Grade A+");
		}
		else if(stdMarks >= 75) {
			System.out.print("Student under Grade A");
		}
		else if(stdMarks >= 40) {
			System.out.print("Student under Grade A-");
		} 
		else {
			System.out.print("No grade");
		}
	}
}

Output:

Student under Grade A

Above line gets printed after executing the example, lets understand how the flow executed.

  1. Declared the student marks as 76.
  2. Checks condition of if, whether 76 is greater than or equals to 90.
  3. Condition is not satisfied, skips execution of if block.
  4. Checks condition of else if, whether 76 is greater than or equals to 75.
  5. Condition satisfied, if else block gets executed and remaining condition checks will be skipped.

The following is the flow diagram of if , if else and else condition shows how flow gets executed.

If else if else flow diagram

Conclusion:

In this tutorial, we have learned about if, if else and if, else if and else conditions with working example along with the flow diagrams.