Branching
Branching statements select which path of a program executes based on a condition.
if
An if statement executes its body when the condition evaluates to true. Conditions must have type bool.
if (temperature > 30)
{
StartCooling();
}
else
An optional else branch executes when the preceding if condition is false.
if (temperature > 30)
{
StartCooling();
}
else
{
KeepRunning();
}
Chained conditions
Place another if after else to test several conditions in order. The first matching branch runs.
if (health == 0)
{
Stop();
}
else if (health < 25)
{
Warn();
}
else
{
Continue();
}