Making decisions in Java
Our Java code will constantly be making decisions. For example, we might need to know whether the user has new messages or whether they have a certain number of friends. We need to be able to test our variables to see whether they meet certain conditions and then execute a certain section of code depending upon whether it did or not.
In this section, as our code gets more in-depth, it helps to present it in a way that makes it more readable. Let's look at code indenting to make our discussion about decisions easier.
Indenting code for clarity
You have probably noticed that the Java code in our projects is indented. For example, the first line of code inside the MainActivity
class is indented by one tab, and that the first line of code is indented inside each method. Here is an annotated diagram to make this clear by way of another quick example:
Also notice that when the indented block has ended, often with a closing curly brace }
that }
, it is indented to the same extent...