Home

if Statement

Single Choice if Statement

C++ is a free-form language, so all of the following are equivalent. The last form is strongly recommended.

if (a > b)
  DoSomething();

if (a > b) DoSomething();

if (a > b) { DoSomething(); }  // block statement on same line

if (a > b)
{
  DoSomething();
}

Multi-choice if statement

Only one if branch at the beginning and one else branch at the end are allowed. Zero or more else if branches are allowed.

if (a > b)
{
  DoSomethingGreaterThan();
} else if (a < b)
{
  DoSomethingLessThan();
} else
{
  DoSomethingEqual();
}

Home


Questions or comments to phil@munts.net