The most complex programming problems can often be boiled down to sets of simple choices that a game or program evaluates and acts on. Since Visual Studio and Unity can't make those choices by themselves, writing out those decisions is up to us. The if-else and switch selection statements let you specify branching paths, based on one or more conditions, and the actions you want to be taken in each case. Traditionally, these conditions include the following:
- Detecting user input
- Evaluating expressions and Boolean logic
- Comparing variables or literal values
The if-else statement
The if-else statements are the most common way of making decisions in code. Stripped of all its syntax the basic idea is if my condition is met, execute this block of code. If it's not, execute this other block of code. Think of these statements as gates, or doors, with the conditions as their keys. In order to...