Introduction to Nested If Statements: Android Studio Crash Course (Free Tutorial)

Is your code cluttered? You know what they say: tidy code, tidy mind! With this article, you’ll learn how to simplify your code. Specifically, we’re going to compare two ways of checking the value of an integer: using several if statements vs using one if statement.

If you need an introduction to coding and want to learn for FREE, check out our 30-minute beginners course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

To follow along with this example in Android Studio, go into Project view. Then go to app > java > (topmost) com.example.zebra.demo > MainActivity.

Firstly we have to set up some variables. On a line underneath setContentView(R.layout.activity_main);, declare an integer number, and set it equal to 98.

int number = 98;

Below that, declare the string message, and set it equal to "This number is ". This is part of the message that we want to display on the screen.

String message = "This number is ";

Next create an if statement:

if(){

}

Within this if statement, we’ll be checking if our integer is greater than 95. In the parentheses, write number > 95.

As well, we need to code the other part of the message that will be displayed on the screen if our number is greater than 95. Within the curly brackets of the if statement, let’s add the following:

message += " greater than 95";

Still within the if statement, let’s build another if statement. We’ll use this nested if statement to check if our integer is less than 100.

if(number < 100){

}

If our integer is less than 100, let’s have the message “This number is greater than 95 and lower than 100.” appear. For this to happen, add the following line within the curly brackets of your second if statement:

message += " less than 100.";

To have our message actually appear on the screen, we will need to use the Toast utility. Below your if statements, type in “Toast”, select “Create a new Toast”, and hit Enter. Android Studio will auto-complete this code:

Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();

Within the parentheses of this code are the parameters. Change the quotation marks to message.

If you run the emulator and zoom in, you will see the text “This number is greater than 95 and less than 100.” appear on the screen, exactly as expected.

But there’s a faster way of doing this operation without using the nested if statement. Cut number < 100 from your nested if statement, and place it into the parentheses of the first if statement. Use the AND operator (&&) to separate the two parameters in the parentheses:

if(number > 95 && number < 100){
message += " greater than 95";
}

Also add and is less than 100. after greater than 95. Now you can delete the nested if statement. As such, with only one if statement, we can check if our integer is between 95 and 100!

Run the emulator again to see the same message appear on the screen. Although most of the time you will need if statements, you were able to write this program in fewer lines of code that are easier to understand.

For more FREE tutorials, check out our 30-minute intro course on coding: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

Author Profile

Alexandra Kropova
Alexandra Kropova
Writer of blog posts and books at Mammoth Interactive. Can also be found posting to Mammoth Interactive's social media.

Leave a Reply