Android Course - If and If Else Statements

Android Tutorial – If and If Else Statement

Articles & Tutorials Programming

Android Tutorial – If and If Else Statement

 

What is if statement? How to use if and if-else statement?

In Any programming language, we need to execute a certain part of code based on a specified condition is true or false. For these cases, control with if or if-else statements is used.

 

Java if Statement:

The syntax of if statement in Java is like this:

if (expression) {
    // statements
}

expression is a boolean that returns only true or false.

If the expression return true, code inside if body is executed.

If the expression return false, code inside if body is skipped.

 

Here is a simple example of how to use if statement in Java.

class ConditionExample {
    public static void main(String[] args) {

    	int age = 38;

    	if (age > 30) {
    		System.out.println("38 is higher than 30");
    	}
    	System.out.println("This code is always executed.");
    }
}

 

When you run the program, the output will be:

38 is higher than 30.

This code is always executed.

 

When age is 38, the test expression age > 30 is evaluated to true. The code inside if statement will be executed.

You can change the value of age to 25 for example and you’ll get this output:

This code is always executed.

When age is 25, the test expression age > 30 is evaluated to false. So, Java compiler skips the code inside the if body.

 

Java if-else Statement:

We have seen in the previous section how if the statement works, sometimes you need to execute another code if the condition returns false so in this case, we add the else statement.

The syntax of the if-else statement is:

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

And here is an example of how to use if-else statement :

class ConditionExample {
   public static void main(String[] args) {    	
      int age = 38;
   
      if (age > 30) {
    		System.out.println("38 is higher than 30");
      } else {
         System.out.println("The age is higher than 30.");
      }
 
      System.out.println("This code is always executed.");
   }
}

 

The output is:

38 is higher than 30.

This code is always executed.

 

When you change the value of age to 25 for example. The result will be:

The age is higher than 30.

This code is always executed.

When age is 25, the test expression age > 30 return false. In this case, the part of codes inside the else body is executed, and part of codes inside the if body is skipped from execution.

 

Java if else if Statement:

In Java, you can use a sequence of if-else statements. The syntax is like this :  

if (expression1) 
{
   // codes 1
}
else if(expression2) 
{
   // codes 2
}
else if (expression3) 
{
   // codes 3
}
.
.
else 
{
   // codes of else
}

 

The if statements are executed from the first to the last.

If all test conditions (expressions) returns false, codes inside the else body are executed.

An example is always good to understand the concept :

class ConditionExample {
   public static void main(String[] args) {   

      int age = 20;
   
      if (age < 18) {
         System.out.println("The age is less than 18.");
      }
      else if (age < 35) {
         System.out.println("The age is less than 35.");
      }
      else {
         System.out.println("The age is higher than 35.");
      } 
   }
}

 

The result will be:

The age is less than 35.

 

You can also have if-else statement inside if or else statement for example :

class ConditionExample {
   public static void main(String[] args) {   

      int age = 20;
   
      if (age < 18) {
         System.out.println("The age is less than 18.");
   if (age < 10) {
          System.out.println("The age is less than 10.");
   
      } else {
   System.out.println("The age is higher than 10.");
      }
      else {
         System.out.println("The age is higher than 18.");
      } 
   }
}

 

Related video :

 

Related posts:

Android Tutorial – Java Array, List, and Map

Android Tutorial – Java Variables

Leave a Reply

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