AppDividend
Latest Code Tutorials

Java If-else Statement | Conditional Statement in Java Tutorial

0

Java If-else Statement | Conditional Statement in Java Tutorial is today’s topic. The Java if the statement is used to test a condition. It checks for the boolean condition: true or false. There are various kinds of if statement in Java. In any programming language, it’s often desirable to execute an individual section of code based upon whether the specific condition is true or false (which is known only during run time). For such cases, the control flow statements are used. If you do not know the OOP concept in Java then please check out Java OOP tutorial.

#Java If Statement

The syntax of the if-then statement in Java is the following.

if (expression) {
    // statements
}

Here, the expression is the boolean expression (returns either true or false).

If an expression is evaluated to true, statement(s) inside a body of if (statements inside parenthesis) is executed.

If an expression is evaluated to a false, statement(s) inside the body of if is skipped from execution.

See the following example.

class Conditionals {
   public static void main(String[] args) {
	int age = 11;
	if (age > 10) {
		System.out.println("Character is most probably Eleven");
	}
	System.out.println("Eleven is always a cute girl in Stanger Things");
   }
}

See the following output.

➜  java javac Conditionals.java
➜  java java Conditionals
Character is most probably Eleven
Eleven is always a cute girl in Stanger Things
➜  java

When the age is 11, the test expression age > 10 is evaluated true. Hence, codes inside a body of the if statements are executed.

Now, change a value of the number to a negative integer. Let’s say -21. The output, in this case, will be the output will be following.

Eleven is always a cute girl in Stanger Things

When age is -21, the test expression age > 11 is evaluated to false. Hence, Java compiler skips the execution of the body of if statement.

#Java If…else Statement

The Java if-else statement also tests a condition. It executes a if block if the condition is true otherwise else block, is executed.

The if statement executes a specific section of the code if a test expression is evaluated to true. The if statement may have the optional else block. Statements inside a body of the else statement are executed if the test expression is evaluated to false.

The syntax of the if-then-else statement is following.

if (expression) {
   // codes
}
else {
  // some other code
}

See the following example.

class Conditionals {
  public static void main(String[] args) {
    int age = 11;
    if (age > 10) {
      System.out.println("Character is most probably Eleven");
    } else {
      System.out.println("She is eva");
    }
  }
}

See the output.

➜  java javac Conditionals.java
➜  java java Conditionals
Character is most probably Eleven
➜  java

When age is 11, the test expression age > 10 is evaluated to true. In this case, the codes inside the body of if are executed, and codes inside the body of else statements are skipped from the execution.

Now, change the value of the age to less than 10 number. Let’s say 7. The output, in this case, will be the following.

She is eva

When the age is 7, the test expression age > 10 is evaluated to false. In this case, codes inside a body of else are executed, and codes inside the body of if statements are skipped from execution.

#Java if…else…if Statement

In Java language, it’s possible to execute one block of code among many. For that, you can use the if..else…if ladder. See the following syntax.

if (expression1) 
{
   // codes
}

else if(expression2) 
{
   // codes
}

else if (expression3) 
{
   // codes
}

else 
{
   // codes
}

The if statements are executed from the top towards a bottom. As soon as that test expression is true, tje codes inside the body of that if statement is executed. Then, the control flow of the program jumps outside the if-else-if ladder. If all the test expressions are false, codes inside that body of else are executed.

class Conditionals {
  public static void main(String[] args) {
    int age = 100;
    if (age > 10 && age < 15) {
      System.out.println("Character is most probably Eleven");
    } else if (age > 40 && age < 50) {
      System.out.println("Character is chief");
    } else {
      System.out.println("Character is Demogorgan");
    }
  }
}

See the following output.

➜  java javac Conditionals.java
➜  java java Conditionals
Character is Demogorgan
➜  java

#Using Ternary Operator

We can also use a ternary operator (? 🙂 to perform the task of the if…else statement. It is the shorthand way to check the condition. If that condition is true, then the result of ? is returned. But, if that condition is false, the result of: is returned.

See the following example code.

class Conditionals {
  public static void main(String[] args) {
    int number = 11;
    // Using ternary operator
    String output = (number % 2 == 0) ? "even number" : "odd number";
    System.out.println(output);
  }
}

See the following output.

➜  java javac Conditionals.java
➜  java java Conditionals
odd number
➜  java

#Java Nested if statement

The nested if statement represents the if block within another if block. Here, the inner if block condition executes only when outer if block condition is true. See the following code.

if(condition){    
     //code to be executed    
        if(condition){  
          //code to be executed    
     }    
}

See the following code example.

class Conditionals {
  public static void main(String[] args) {
    int age = 20;
    int weight = 80;
    // applying condition on age and weight
    if (age >= 18) {
      if (weight > 50) {
        System.out.println("You are eligible to donate the blood");
      }
    }
    else {
      System.out.println("You are not eligible to donate the blood");
    }
  }
}

See the following output.

➜  java javac Conditionals.java
➜  java java Conditionals
You are eligible to donate the blood
➜  java

#Java Conditions

Java supports usual logical conditions from mathematics. See some of the following.

  1. Less than: x < y
  2. Less than or equal to x <= y
  3. Greater than: x > y
  4. Greater than or equal to x >= y
  5. Equal to x == y
  6. Not Equal to: x != y

You can use these conditions to perform the different actions for the different decisions.

Java has the following conditional statements:

  1. Use the if to specify a block of code to be executed, if a specified condition is true.
  2. Use the else to specify a block of code to be executed if the same condition is false.
  3. Use the else if to specify a new condition to test, if the first condition is false.
  4. Use the switch to specify many alternative blocks of code to be executed.

Finally, Java If-else Statement | Conditional Statement in Java Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.