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;
}