Tsallaka zuwa abun ciki
Justice Tech School

If and Switch

if, else if and else choose between branches. switch picks between many values and needs break after each case. The ternary operator is a short if/else.

Example
int score = 72;
if (score >= 85) {
  cout << "Distinction" << endl;
} else if (score >= 70) {
  cout << "Pass" << endl;
} else {
  cout << "Retake" << endl;
}
int day = 3;
switch (day) {
  case 1: cout << "Monday" << endl; break;
  case 3: cout << "Wednesday" << endl; break;
  default: cout << "Other" << endl;
}

Result

Pass
Wednesday

Exercise

Run when the condition is false.

if (ok) {}  {}

Quiz Test

What does break do in a switch case?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.