Skip to main content

5.1) Simple If Statement


Flow of Control


Example

if (x < 0) {
  cout << "x is negative" << endl;
  exit(1);
}
// Continue with program here

Key Points

  • Test must be enclosed in ( )
  • Statements to be executed if test is true go in { }
  • If only a single statement needs to be executed when the test is true, the { } can be omitted