codegurukool.com · Free coding tutorials & online compiler

Home › C++ Tutorial › C++ if...else and switch

C++ if...else and switch

Make decisions.

Use if, else if, else, the ternary ?: and switch with break.

Example code

#include <iostream>
using namespace std;
int main() {
  int t = 18, d = 3;
  if (t < 12) cout << "Morning" << endl;
  else cout << "Later" << endl;
  switch (d) {
    case 1: cout << "Mon" << endl; break;
    case 3: cout << "Wed" << endl; break;
    default: cout << "Other" << endl;
  }
  return 0;
}

Run this code in the online editor

← C++ mathC++ while loop →