Java - If Statements and Branching your Code
Learning to take your program in different directions
Video Reference: youtu.be/BZbPSpgPsVs
Book Reference: pg. 87-90
An if statement is a statement in code that evaluates a condition and if that condition is true, it will run the block of code in the branch.
If statements are one of a couple of "Control Flow" statements available in the Java language. Controlling the flow of your program is the concept of your code dynamically taking a different route and running differently depending on the conditions of the program, say some number you got from input or anything.
Considering how basic programs you will be coding up to this point are, this is a revolutionary concept. I highly recommend trying out these code snippets in your own program as you read this.
Anatomy of an If Statement
If the condition within the parenthesis evals to true, the code in the brackets or the branch will run. Once the code inside is done executing, the program will continue running where it was before directly after the if statement.
if (condition){
//code
}
In a world in which people my age are cool, you can write this code to let them know that they are cool:
int myAge = 20;
int requiredAge = 20;
if (myAge == requiredAge){
System.out.println("Wow, you are really cool.");
}
In these basic examples, you can see that the program will always execute the same way simply because the conditions are never changing, the variables that are within the condition being evaluated never change. In the near future, you will see that our variables have the capability of changing dynamically as the program goes on, therefore the program may not always take the same path due to changing conditions.
Else Branch
If you want a different branch of code to run if the condition in the if statement is not true, you can add an else on the end.
if (myAge == requiredAge){
System.out.println("Wow, you are really cool.");
}else{
System.out.println("You suck.");
}
Else-If Branches
With the current knowledge gained from reading this article, you now are able to create control flow statements that run code based on a single condition only, it runs one branch if the condition is true, and another if it is false.
You can make your code even more complex by chaining together more elses and ifs into the mix like so:
if (myAge < 18){
System.out.println("You are a minor.");
}else if (myAge >= 18){
System.out.println("You are an adult.");
}
The difference between this and the last code snippet is that an else without an if after it will always execute ifs above it are false. In the above snippet, it will print "You are an adult." only if the first condition is false and if the second condition is true.
The cool thing is that you can add as many as you want:
if (myAge < 18){
System.out.println("You are a minor.");
}else if (myAge >= 18 && myAge < 70){
System.out.println("You are an adult.");
}else if (myAge >= 70 && myAge < 100){
System.out.println("Wow, you are getting old.");
}else if (myAge >= 100){
System.out.println("You are still alive?!?! That is impressive.");
}
Even if you want to have a bunch of different conditions chained together, you will often need some code to run regardless, since all of the conditions were false. To do that, you can throw an else on the end as you did before:
if (myAge < 18){
System.out.println("You are a minor.");
}else if (myAge >= 18 && myAge < 70){
System.out.println("You are an adult.");
}else if (myAge >= 70 && myAge < 100){
System.out.println("Wow, you are getting old.");
}else{
System.out.println("You are still alive?!?! That is impressive.");
}
I just changed the last else if condition to else and removed the condition since if all the conditions above that are false, the age must be >= 100.
Nested If-Statements
When you put one if statement within another, that is known as nesting. Just like a Russian nesting doll. The more nesting you do, the more complex your program can get.
if (myAge < 18){ //if myAge is less than 18
System.out.println("You are a minor.");
}else if (myAge >= 18 && myAge < 70){ //if myAge is greater than or equal to 18 and less than 70
System.out.println("You are an adult.");
if (myAge == 20){ //if myAge is equal to 20
System.out.println("You are 20.");
}
}
Single Line If Statements for Conciseness
If the code within the if statement is only one line long, you don't need the brackets. I don't happen to like this style of if statements because it makes it less readable in my brain, but that design decision is always up to you.
if(1 + 1 == 2)
System.out.println("1 + 1 = 2");
else
System.out.println("1 + 1 != 2");
//More examples
if(1 + 1 == 2)
System.out.println("1 + 1 = 2");
else if(1 + 1 == 3)
System.out.println("1 + 1 = 3");
else
System.out.println("1 + 1 != 2 or 3");
//A nested example
if(1 + 1 == 2)
if(1 + 1 != 3) System.out.println("1 + 1 != 3");
else if(1 + 1 == 4) System.out.println("1 + 1 = 4");
else System.out.println("1 + 1 != 2 or 3 or 4");
Conclusion
Now you know how to take your program in different directions, a powerful skill to have. If you have any questions, leave it below as a comment or hop into our discord community. In the next article you can expect a similar control flow statement known as the switch statement.