If Statements
Curly Braces - Syntax Rules
- Curly braces are optional for a then part that is a single statement
- Curly braces are required for a then part that contains more than one statement
- Curly braces are optional for an else part that is a single statement
- Curly braces are required for an else part that contains more than one statement
Truth in Advertising
- Actually, {statement1; statement2; ...} is itself a kind of statement
- So we can say that curly braces are never required
Curly Braces - In Practice
- Always use curly braces
- This will help avoid the error of incorrectly adding a statement
- Exception: the else if construction:
if (a == b)
{
some statements
}
else if (a == c)
{
more statements
}
else
{
even more statements
}