Decision Statements : Java If Else

Java if else Java If Else

If Else decision statements help you to control the flow of your program execution and let you decide what should be executed on a particular condition. Java has provided us “If-Else” decision statement to implement this.

“If-Else” is a decision statement which takes boolean expressions as input. Based on the boolean value whether it is true or false, your block of code will be executed.

Let’s take a look on execution flow of If Else


Java if else



How it works :
  • Program enters inside if conditional block.
  • The expressions are evaluated inside the conditional block.
  • Based on the expression result (whether it is true or false), your code will be executed.
  • If condition is true then your code inside the if block will be executed.
  • If condition is false then your code inside else block will be executed, code inside if block will not be executed.

Types of If Else Conditions
  1. If Statement
  2. If else Statement
  3. Nested If Statement (If inside If)
  4. If – Else – If Statement

1. If Statement

If Statement is a decision statement in which your code will only be executed if the condition is true.

Flow Diagram



First you expression will be evaluated by if condition. If result is true then code inside if block will be executed. Otherwise, it will be neglected.

Example
public class IfElseExample {

 public static void main(String[] args) {

 if (2 > 1) {
 System.out.println("Condition is true, you are inside if block.");
 }

 if (true) {
 System.out.println("Condition is true, you are inside if block.");
 }

 if (false) {
 System.out.println("Condition is false, this code of block will not be executed.");
 }

 // You can not use primitive value as input, you can only use boolean
 // values, following code will not work
 if (22) {
 System.out.println("you can not use premitive values inside if block.");
 }

 // Multiple conditions with Bitwise AND operator
 if ((3 > 1) && (2 < 4)) {
 System.out.println("Code will only execute if both conditions are true.");
 }

 // Multiple conditions with Bitwise Or Operator
 if ((3 > 1) || (2 > 4)) {
 System.out.println("Code will only execute if only one condition is true.");
 }
 }

}

2. If Else Condition

If Else is a decision statement in which your code inside if block will only be executed when if condition is true otherwise your else block will be executed.

Flow Diagram


Java if else



First your conditions will be evaluated, if condition is true then it will go inside if block otherwise it will neglect the if block and move on to the else block.

Example
public class IfElseExample {

 public static void main(String[] args) {

 
 if(23 > 1) {
 System.out.println("Inside if block."); // This will be the output
 } else {
 System.out.println("Inside Else block.");
 }
 
 }

}

3. Nested If Condition (If inside If)

Nested if is a decision statement same as if condition. However, we can include more if blocks inside a particular if condition.

Flow Diagram



In above flow diagram, first if condition is evaluated. If first condition is false then nested if will not be evaluated and will exit. If first condition is true then second decision statement will be evaluated if it is true then code inside nested if will be executed otherwise it will exit.
Example

public class IfElseExample {

 public static void main(String[] args) {

 if (22 > 20) {

 if ((22 % 2) == 0) {
 System.out.println("22 % 2 has given 0.");
 }

 }

 }

}

4. If Else if Statement

You can create multiple if – else – if statements to check multiple conditions. There will only be one statement from multiple if – else – if which will be executed.

Flow Diagram



In above diagram, first if condition will be checked. It it is true then code inside if condition will be executed and rest will be neglected. If first condition is false then it will check next else if condition, if it is true then it’s code will be executed and rest will be neglected and so on.

Example
public class DataTypeExample {

 public static void main(String[] args) {

 String days = "Mon";
 
 if(days == "Sun") {
 System.out.println("Today is sunday.");
 } else if(days == "Mon") {
 System.out.println("Today is Monday.");
 } else if(days == "Tue") {
 System.out.println("Today is Tuesday.");
 } else if(days == "Wed") {
 System.out.println("Today is Wednesday.");
 } else if(days == "Thu") {
 System.out.println("Today is Thursday.");
 } else if(days == "Fri") {
 System.out.println("Today is Friday.");
 } else if(days == "Sat") {
 System.out.println("Today is Saturday.");
 }

 }

}

Leave a Reply

Your email address will not be published. Required fields are marked *