Lesson 14: if / else / else if
Now we turn a condition into a decision. In C#: if (condition) { ... } runs the block only if the condition is true. else { ... } runs when it's false, and else if (another) checks a further option. Note the C# syntax: the condition is in round parentheses ( ), and the body is in curly braces { } —
if is a fork in the road: if the condition is true you go into the if block; otherwise into the else block. else if adds another fork.
- if (condition)
- Runs the block after it only if the condition is true. The condition is in parentheses.
- else
- The block that runs when the if's condition is false.
- else if
- Checks a further condition if the previous one didn't hold.
- block { }
- The curly braces wrapping the if/else body. The block itself doesn't end with ;.