codingstreets
Search
Close this search box.

Get Started: Java If Else

In This Article, You Will Learn about Java If Else.

Java Booleans – Before moving ahead, let’s know a bit about Java Boolean.

Table of Contents

Java If Else

In many situations, you must pick only one alternative (yes or no, true or false, correct or incorrect) in response to a specific condition. Java also uses statements based on certain conditions to indicate the best option for you to select.

Java If Statements

Java has the following conditional statements:

  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the existing condition is false
  • Use else if to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative blocks of code to be executed

Java Conditions

Java uses the logical operators from the mathematical operators to put the conditions between statements-

  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equal to a == b
  • Not Equal to: a != b

Java if Statement

Use the if statement to specify a block of code (condition) to be executed, if a condition is true.

Syntax

				
					if (condition) {
  // block of code to be executed if the condition is true
}
				
			

Note: Java is a case-sensitive language. Lowercase if and Uppercase IF are different.

Note:  An if statement is written with ‘if’ keyword.

Example: Use if condition with greater than (>) operator to check which number is greater than from other number. 

				
					public class Main {
  public static void main(String[] args) {
    if (200 > 108) {
      System.out.println("200 is greater than 108");
    }  
  }
}
				
			

We can write above same condition in this way too –

				
					public class Main {
  public static void main(String[] args) {
    int x = 200;
    int y = 108;
    if (x > y) {
      System.out.println("x is greater than y");
    }  
  }
}
				
			

Example explained

In the example above we use two variables, x and y, to test whether x is greater than y (using the > operator). As x is 200, and y is 108, and we know that 200 is greater than 108, we execute that “x is greater than y”.

Example: Use if condition with equal (==) operator to check whether number is equal to or not.

				
					public class Main {
  public static void main(String[] args) {
    if (200 == 200) {
      System.out.println("200 is equal to 200");
    } else {
      System.out.println("200 is not equal to 200.");
    }    
  }
}
				
			

Example: Use if condition with equal (==) operator to check whether number is equal to or not.

				
					public class Main {
  public static void main(String[] args) {
    if (200 != 108) {
      System.out.println("200 is not equal to 108");
    } else {
      System.out.println("Both numbers are different.");
    }  
  }
}
				
			

Java else Statement

Use the else statement to specify a block of code to be executed if the condition is false.

Syntax

				
					if (condition) {
  // block of code to be executed if the condition is true
} else {
  // block of code to be executed if the condition is false
}
				
			

Example:  Use if… esle condition with less than (<) operator to check weather is good or not.

				
					public class Main {
  public static void main(String[] args) {
    int weather = 50;
    if (weather < 25) {
      System.out.println("It is normal outside today.");
    } else {
      System.out.println("It is too hot outside today.");
    }  
  }
}
				
			

Example explained

In the example above, weather (50) is greater than 25, so the condition is false. Because of this, we move on to the else condition and execute, “It is too hot outside today.”. If the weather was less than 25, the program would execute “It is normal outside today.”

Java else if Statement

Use the else if statement to specify a new condition if the first condition is false.

Syntax

				
					if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else {
  // block of code to be executed if the condition1 is false and condition2 is false
}
				
			

Example:  Use if… esle if… else condition with equal (==) operator to check weather is good or not.

				
					public class Main {
  public static void main(String[] args) {
    int weather = 50;
    if (weather > 50) {
      System.out.println("It is too hot outside today..");
    } else if (weather  < 25) {
      System.out.println("It is mid-level hot outside today.");
    }  else {
      System.out.println("It is hot outside today.");
    }
  }
}
				
			

Example explained

In the example above, weather (50) is greater than 50, so the first condition is false. The following condition, in the else, if statement, is also false, so we move on to the else condition since condition1 and condition2 are both false – and execute “It is hot outside today.”

Example:  Use if… esle if… else condition with greater than or equal to (>) and less than or equal to (<=) operator to check weather is good or not.  

				
					public class Main {
  public static void main(String[] args) {
    int weather = 50;
    if (weather >= 50) {
      System.out.println("It is too hot outside today..");
    } else if (weather <= 25) {
      System.out.println("It is mid-level hot outside today.");
    } else {
      System.out.println("It is normal outside today.");
    }
  }
}
				
			

Java Short Hand If… Else

Short Hand If… Else

Short Hand If… Else is a shortcut of if else, which is known as the ternary operator because it consists of three operands.

In Java, Short Hand If… Else is used to replace the multilines of block of code with a single line and also it is used often to replace the if else statements.

Syntax

				
					variable = (condition) ? expressionTrue :  expressionFalse;
				
			

Example:

				
					public class Main {
  public static void main(String[] args) {
    int weather = 50;
    if (weather < 25) {
      System.out.println("It is normal outside today.");
    } else {
      System.out.println("It is too hot outside today.");
    }  
  }
}
				
			

Instead of above code, write like this:

				
					int weather = 50;
String output = (weather < 25) ? ("It is normal outside today.") : ("It is normal outside today.");
System.out.println(output);

				
			

Example:

				
					public class Main {
  public static void main(String[] args) {
    int number = 200;
    if (number != 108) {
      System.out.println("200 is not equal to 108");
    } else {
      System.out.println("Both numbers are different.");
    }  
  }
}
				
			

Instead of above code, write like this:

				
					number = 200;
String output = (number != 108) ? ("200 is not equal to 108") : ("Both numbers are different.");
System.out.println(output);
				
			

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on