Skip to main content

5.3) If Statement: Multiple Branches


Example

if (x < 0) {
  cout << "x is negative" << endl;
  exit(1);
}
else if (x == 0) {
  // Handle special case where x is zero
}
else {
  // Do calculation with x here
}

Key Points

  • if clause can be followed by else if clauses, each of which must have a test
  • else clause is optional but, if used, must come at the end