When we are talking about conditionals, we want to control the flow of the program. E.g. if a condition is True we want instruction to be executed otherwise we do not want to execute that.
if (condition) { // block of code to be executed if the condition is true
}
If...else
In if...else statement, if the condition is True, we will execute the first block of code and if it is False, we will execute the second block of code.
if (condition) { // block of code to be executed if the condition is true (first block of code)
} else { // block of code to be executed if the condition is false (second block of code)
}
If else if else Statement
You can use else if to check more than one condition if the first condition is False.
if (condition1) { // block of code to be executed if condition1 is true
} elseif (condition2) { // block of code to be executed if the condition1 is false and condition2 is true } else { // block of code to be executed if the condition1 is false and condition2 is false
}
Switch statement
If you have multiple code blocks and you want to choose one of the code blocks to be executed you can use switch statement.