If Statements
If
statements allow you to build programs that can make decisions based on certain conditions.
Let’s take our previous temperature scenario:
- If temperature is greater than 30
- Print “It’s a hot day”, “Drink plenty of water!”
- Otherwise if it’s between 20 and 30
- Print “It’s a nice day”
- Otherwise
- Print “It’s cold (brr)”
int temp = 32;if (temp > 30) { System.out.println("It's a hot day"); System.out.println("Drink plenty of water!");}else if (temp > 20) /*Note the lack of braces here - for single line returns, braces are optional and down to preference.*/ System.out.println("It's a nice day");else System.out.println("It's cold (brr)");
A few things to note:
- Single line returns do not require a curly brace wrapper, this is down to personal preference.
- Using proper indentation is a great way to make your code easier to read.
- The conditions are evaluated top to bottom, so ensuring the correct order is important for reducing redundancy in your code.
Note: Mosh does mention the lack of curly braces being controversial whilst I understand his opinion on reducing code and keeping things clean, I tend to lean on the side of using braces and here’s why.
- Chances of you coming back to clean up code or add functionality in a scope is high therefore preparing for this early means less likely hood of having errors.
- I find it visually easier to follow what’s in the scope of the condition (where it starts and ends) - Most IDE’s and markup language’s also make it easy to see this.
- If you are dealing with a group that might not know the nuances of Java specifically they might be confused by a mismatch in standards used in your code base.
Moving forward I will provide snippets with bracers but again. It comes down to personal preference!
Simplifying if statements
Section titled “Simplifying if statements”Let’s have a look at some code:
int income = 120_000;if (income > 100_000) { boolean hasHighIncome = true;}
This will return an error as Java only allows declarations within code blocks defined by curly braces
int income = 120_000;if (income > 100_000) { boolean hasHighIncome = true;}
Now we have a new problem in Java variables are scoped from parent block down, as our variable has been defined in the if
statements code block accessing that variable will only be available and scoped to the if
statement and it’s child blocks.
int income = 120_000;boolean hasHighIncome;if (income > 100_000) { hasHighIncome = true;} else { hasHighIncome = false;}
Although this works, there is some redundancy in the else
clause. Specifically, we’re setting hasHighIncome
to false
in the else
block, but we can simplify this by directly initialising the variable with a default value before the if
statement. This removes the need for the else
block entirely, making the code more compact and easier to read.
int income = 120_000;boolean hasHighIncome = false;if (income > 100_000) { hasHighIncome = true;}
Now we no longer need the else
clause making the code more compact and easier to read. Java does have a way to further optimise situations like this, lets have a look:
int income = 120_000;boolean hasHighIncome = (income > 100_000);
And here we have implemented a simple, elegant professional way to represent a boolean representation of income being above 100,000.
Note: Mosh also provides the guidance of wrapping our Boolean expression in parenthesis. Whilst it’s not necessary I completely agree with why we should do this as it does make our code easier to read.