Tsallaka zuwa abun ciki
Justice Tech School

Loops

for has start, condition and step. while checks first and do...while runs once before checking. A range-based for loop walks through every item of a container. break and continue control the flow.

Example
for (int i = 1; i <= 3; i++) {
  cout << "i = " << i << endl;
}
vector<string> names = {"Ada", "Tunde", "Zainab"};
for (const string& n : names) {
  cout << n << endl;
}
int n = 3;
while (n > 0) {
  cout << n-- << " ";
}
cout << endl;

Result

i = 1
i = 2
i = 3
Ada
Tunde
Zainab
3 2 1 

Exercise

Walk through each item.

for (auto n  names) {}

Quiz Test

Which loop always runs at least once?

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