Exploring if statements
As their name suggests, conditional statements are based on the evaluation of a condition. The result of this condition is either true
or false
– in other words, a boolean. Figure 4.2 introduces the syntax of the overall if
statement:
Figure 4.2 – The if statement syntax
The square brackets []
in the preceding figure denote something as optional. For example, both the else if
statements and the else
statement are optional. The if
statement itself is mandatory. The three ellipses, ...
, indicate that you can have as many else if
statements as you like (or none at all).
Now that we have the overall syntax, let us break it down into smaller pieces.
The if statement itself
As stated earlier, an if
statement evaluates a boolean
expression. This boolean
expression is enclosed in parentheses. Curly braces that delimit a block of code are optional if there is only one statement after the if
clause. However, it is considered...