One of the most basic, yet most important, control flow statements is if. This simple keyword is at the heart of all logic, allowing us to perform a given action only if a specified condition is true. By chaining these if
statements together in creative ways, we can model any logical system.
The syntax for an if
statement is as follows:
if (condition) { // do stuff. }
If the statement we use as our condition resolves to true
, then the code within the curly braces will be executed. If the statement is false
, then it will be skipped. Our condition can be anything that can be either true or false. This can be something simple, such as checking the value of a Boolean, or something more complex, such as the result of another operation or function.
We also have the else
statement. This allows code to be executed if, and only if, a preceding if
statement's condition evaluates to false
. If the condition evaluates to true, however, and the if...