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.
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!