Java If else Statement

Learners can benefit from this post to better understand Java If Else Statement with examplesJava if statement tests the condition.

Java If else Statement

True or false is checked based on a boolean condition. A Java if statement can be of various types.

  • if statement.
  • if-else statement.
  • if-else-if ladder.
  • nested if statement.

Java If else Conditions

In terms of Java if else conditions, Java supports the usual logical conditions from mathematics:

  • Less than: mrx < ample
  • Less than or equal to: mrx <= ample
  • Greater than: mrx > ample
  • Greater than or equal to: mrx >= ample
  • Equal to : mrx == ample
  • Not Equal to: number 1 != ample

These conditions can be used to perform different actions for different decisions.

In Java, conditional statements are as follows:

  • Using if, a block of code is executed if a condition is met.
  • The else statement specifies a block of code that will run if the same condition is not true.
  • If the first condition is false, use else if to test another condition.
  • Switches can be used to specify a variety of alternative code blocks


if Statement Java

For Java if else conditions, use the if statement to specify how Java code will be executed if a condition is met.

if Syntax:

if (Checking Condition) {
// If the condition is true, execute this block of code.
}

if must be written in lowercase. For if Statement in Java, uppercase letters (If or IF) will generate an error.

Below is an example of testing two values to see if mrx is greater than ample . Whenever the condition is true, print the following text:

Example: 

public class Main { public static void main(String[] args) { if (20 < 28) { System.out.println("20 is less than 28"); // 20 is obviously less than 28 } } }

Example: 

public class Main { public static void main(String[] args) {if (50 < 165) { System.out.println("165 is greater than 50"); } } // Returns true }

Variables can also be tested:

Example: 

public class Main { public static void main(String[] args) { int mrx =10; int ample=20; if (ample > mrx) { System.out.println(ample +" is greater than "+mrx); // 20 is greater than 10 } } }

Above Example Explanation:

The example above uses two variables, mrx and ample, to test whether ample is greater than mrx (using the greater than ( > ) operator). As mrx is 10, and ample is 20, and we know that 20 is greater than 10, we print to the screen that “ample is greater than mrx”.

Example: 

public class Main { public static void main(String[] args) {int ample =1120; int mrx=2410;if (ample < mrx) { System.out.println(ample +" is less than "+mrx); // 2410 is greater than 1120 } } }

Java If else example


The else Statement

As with Java if else conditions, you can specify a block of code to be executed if the condition is false by using the else statement.

Syntax

if (condition) { 
// code to be executed if the condition is satisfied
} else { 
// code to be executed if the condition is not satisfied
}

Example: 

public class Main { public static void main(String[] args) { int boys = 40; int girls=28; if (boys < girls) { System.out.println("Girls are more in number than boys"); } else { System.out.println("Boys are more in number than girls"); } } }

 

Above Example Explanation:

The if condition is false in the case above because boys (40) are greater than girls (28).

As a result, we go on to the next condition and print “Boys are more in number than girls” on the screen.

The computer would print “Girls are more in number than boys”, if the number of boys were less than 28.

Example: 

public class Main { public static void main(String[] args) { int required_Age = 30 ; int user_Age=45;if (user_Age < required_Age ) { System.out.println("We are Sorry to inform you that your age is less than the required age for this job"); } else { System.out.println("We are glad to inform you that you are eligible for this job"); } } }

The else if

In Java if else statements , we describe a new condition using the else if statement. If the initial condition is false.

Syntax:

if (condition1) { // if condition 1 is true, this block of code is executed
} else if (condition2) {
// if condition 1 is false and condition 2 is true, this block of code is executed.
} else {
// block of code that will run if both condition 1 and condition 2 are false
}

Example: 

public class Main { public static void main(String[] args) { int marks = 27; if (marks>0 && marks < 20) { System.out.println("You need to work hard to achieve good grades"); } else if (marks>=20 && marks < 35) { System.out.println("Nice job ! Keep working hard !!"); } else { System.out.println("Excellent Performance"); } } }

Above Example Explanation:

In the example above, marks (27) are greater than 0 and less than 20, so the first statement is false. The next condition, Whereas the else if statement is true as marks(27) are greater than equal to 20 and less than 35, hence the part of code in the else-if block is executed, so the program will not move to the else statement in this scenario.

However, if the marks were greater than 0 and less than 20, our program would print “You need to work hard to achieve good grades.” Similarly if the marks were greater than 35 , our program would print “Excellent Performance.”

Example: 

public class Main { public static void main(String[] args) {int required_Age=20; int user_Age=44; if(user_Age ==0 ){ System.out.println("Wrong age input !!"); } else if(user_Age > 1 && user_Age <=19){ System.out.println("Sorry !! your age does not meet the set criteria"); } else{ System.out.println("Congratulations ! You have successfully met our set criteria of age"); } } }

 


Java If-Else (Ternary Operator)

There is also a short-hand if-else, which is known as the ternary operator since it has three operands only. You can use it to replace multiple lines of code with one line. When writing Java if else conditions, it is often used to replace simple if-else statements:

Syntax:

variable = (checking_condition) ? expressionTrue : expressionFalse;

Rather than writing:

Example: 

public class Main { public static void main(String[] args) { int candidate_age = 26; if (candidate_age < 18) { System.out.println("Sorry !! You are not eligible to cast your vote"); } else { System.out.println("Congratulations !! You are eligible to cast your vote"); } } // Output: Congratulations !! You are eligible to cast your vote }

Java If Else Ternary Operator

The following can be written simply:

Example: 

public class Main { public static void main(String[] args) { int candidate_age = 30; String result = (candidate_age < 18) ? "Sorry !! You are not eligible to cast your vote" : "Congratulations !! You are eligible to cast your vote"; System.out.println(result); } }

Example: 

public class Main { public static void main(String[] args) {int girls_number=35; int boys_number=27;if(boys_number == girls_number ){ System.out.println("Boys are equal in number to the girls !!"); } else{ System.out.println("Boys are either lesser or more in numbers than the girls but not equal "); } } }

This code can also be simplify as:

Example: 

public class Main { public static void main(String[] args) {int girls_number = 35; int boys_number = 27;String result = (boys_number == girls_number) ? "Boys are equal in number to the girls !!" : "Boys are either lesser or more in numbers than the girls but not equal ";System.out.println(result); } }

 

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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