Tsallaka zuwa abun ciki
Justice Tech School

switch, Ternary and Range For

switch matches integers and enums, the ternary operator is a short if, and a range based for loops over containers.

Example
int day = 3;
switch (day) { case 1: cout << "Mon\n"; break; case 3: cout << "Wed\n"; break; default: cout << "Other\n"; }
cout << (day > 2 ? "late" : "early") << "\n";
vector<int> v = {1, 2, 3};
for (int x : v) cout << x << " ";
cout << "\n";

Result

Wed
late
1 2 3 

Exercise

Loop over a container.

for (int x  v)

Quiz Test

What is a range based for?

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