# Nested IF statements

One of my readers is in trouble and has a quiz on this topic, so I thought I would try my best to help him (as well as everyone) out by posting another Java tutorial today. So! Here’s a quick tutorial on the IF statement and some of its more complex uses.

## The nested IF

I’m sure at this point you are all fairly comfortable with the `if` statement as it’s the most basic control structure in the Java programming language (and other languages too). But were you aware that you could nest these `if` statements inside of each other?

How about I first introduce a simple example to refresh your memory!

```int age = 29;   if (age < 19) { System.out.println("You are not an adult."); } else { System.out.println("You are an adult."); }```

As you can see here we have a standard `if..else` structure. In this particular example I’ve set the `age` variable equal to the `int` value 29. So, since you are very keen with your Java knowledge, I bet you’d guess that the console output would read “You are an adult.” Nice! So let’s get a little more complicated shall we?

```int age = 29;   if (age < 13) { System.out.println("You are but a wee child!"); } else if (age < 19) { System.out.println("You are no longer a child, but a budding teenager."); } else { if (age < 65) { System.out.println("You are an adult!"); } else { System.out.println("You are now a senior, enjoy the good life friends!"); } System.out.println("Also, since you are over the age of 19, you deserve a drink!"); }```

Alright! So, you see here how I made use of a nested `if` statement? Since my `age` variable is still set as 29, the code will flow to the first `if` and check to see if the `age` is less than 13. We all know that 29 is not less than 13, so we continue downward to the first `else if`. Now We check if `age` is less than 19, it’s not, so we continue to the next `else if`. But here’s where it gets interesting, there are no more `else if` statements, all we have left is an `else` block. So, what happens?

Well, the same rules always apply to the flow of code in an `if..else` condition. If the none of the conditions evaluate to `true` in all the `if` conditions, then the code will automatically choose the `else` path if it exists. In our example, it does indeed exist. So the code will choose that path. And what do you know, we’ve now encountered another `if..else` block of code. So, as predictable as the sun will rise and set, Java will follow the same set of rules for this new `if..else` block.

We will now evaluate if `age` is less than 65. And as luck would have it, our variable is set to 29. So, yes, 29 is less than 65, so we’ll execute the code inside of this block and the console will output “You are an adult.”

### Now, here’s where it might get tricky to some. Where does the code flow from here?!

You have to consider the fact that you are now inside of nested `if` statements and make note of where the beginning and end of all of those curly braces {} are. Allow me to re-write the code block with some comments that may help you visualize where the code will flow.

```int age = 29;   if (age < 13) { System.out.println("You are but a wee child!"); }// end if for age < 13 else if (age < 19) { System.out.println("You are no longer a child, but a budding teenager."); } // end else if for age < 19 else { if (age < 65) { System.out.println("You are an adult!"); }// end if for age < 65 else { System.out.println("You are now a senior, enjoy the good life friends!"); } // end if for nested else System.out.println("Also, since you are over the age of 19, you deserve a drink!"); }// end of final else```

So, since the `age` is set to 29, the code will NOT execute the nested `else` condition. Because if it did, Java would be violating it’s most basic `if..else` rules for code flow right? So instead it will skip over the `else` block. And what do we see after the inner `else` block? Yet another console output line! So you will see this output on your console too: “Also, since you are over the age of 19, you deserve a drink!”.

This gets outputted because we know we’re inside that outer `else` block of code. Remember to follow through those curly braces {} to know exactly where you are.

The code then exits the outer `else` code block and the program will terminate.

## Want Free Access to my Best Java Courses?

Alright, so if you’re read this far down the article, then you’re clearly interested in learning how to code. You can currently get access to my 2 best courses on Java for free for 30 days (then it’s just \$10/month afterwards, or \$97/year).

When you sign up you’ll receive login information to start learning about Java. If you decide you don’t like the courses, you can cancel at any time. If you decide you love the courses then you don’t have to do anything and you can stay subscribed for as long as you like for just \$10/month or \$97/year.

It’s like Netflix for coders!

Go ahead an click the button below to get started!

## Summary

Nesting `if` statements is a fairly straight-forward concept that allows you to have good control over what code gets executed (and NOT executed) in certain scenarios. If you would like a good understanding of how Java reads through line by line, I’d recommend watching my video on Constructors. I show you how the code flows in the debugger. And, if you don’t know how to debug yet, I’d also recommend watching this video!

I hope that helps out my reader for his quiz tomorrow, and I look forward to hearing that he aced it! But as always, if anyone is still unclear on something that I’ve outlined here, please leave a comment below 🙂

Take care everyone!

• George March 13, 2014, 5:18 pm

How and when do I use the try and catch finally with my code?

• Trevor Page March 19, 2014, 4:16 pm

Two resources for you:

• armaan December 11, 2015, 7:36 am

m sry ….bt it didnt helped me

• Ti February 10, 2016, 7:10 am

All examples i hve seen so far of nested if statements use intgers , can you do one with strings please

• Trevor Page February 25, 2016, 10:38 am

Great questions Strings can be a bit more tricky as you can’t just use the `==` operator to compare Strings. Here’s an example of a nested IF with Strings:

```String msg = "A Message";
String msg2 = "another message"; // Note: lower case
String msg3 = "Another Message"; // Note: upper case

if ("A Message".equals(msg))
{
if (msg2.equals(msg3))
{
// it won't ever get here as msg2 doesn't equal msg3 due to case sensitivity in the .equals() method
}
else if (msg2.equalsIgnoreCase(msg3))
{
// it WILL go here because we are ignoring case
}
}
```
• Tim April 4, 2016, 3:46 pm

The formatting is wrong on this page, I dont know if you’re using equals signs or less than size which makes the code quite hard to understand.

• Trevor Page April 7, 2016, 9:50 am

Thanks for letting me know, I’ve updated the page so it’s human readable 🙂

• Harsh Aditya April 24, 2016, 3:05 am

plz explain me basic concecpts of nested if else programming bcoz it’s a li’l tricky!!!!!

• prafull July 9, 2016, 11:49 pm

trevor can you please tell me, how to put logic in programs it is the hardest part for me in programming syntax and all thing i can understand except the logic part please reply or mail me.

• Anwaya November 25, 2016, 5:57 am

This is helpful but could you please add examples with alphabets like a>b.

• Anonymous December 22, 2016, 2:16 pm

This really helped. Thank you

• SHI BIN DAS July 31, 2017, 6:03 am

It’s more hepfull to know java in deep..
but some examples not working properly