As we saw in earlier lessons, the if statement allows us to run a block of code only when a condition is true. Here's the basic structure:
if (condition) {
// This code runs when condition is true
// We can write as much code as we want here
}An if statement always begins with the word if followed by a condition in parenthesis. The condition should be an expression that evaluates to a boolean value. The following are examples of valid conditions:
After the condition, we write code inside curly braces { }. The code inside the curly braces runs only when the condition evaluates to true.
You can write as much code as you want inside the curly braces. In fact, you could write another if statement inside the block too if you wanted to! We'll see examples of this in future lessons.
Output (Console)