Notes / C++ / Loops

Loops in C++ — Revision Notes

A quick-reference cheat sheet: definition, syntax, example, common mistakes and an interview tip.

beginner 📖 2 min revision

Definition

📌 Loops C++ supports the same for, while, and do-while loops as C, plus a modern addition: the range-based for loop, which iterates directly over a container's elements without manual indexing..

Syntax

syntax
for (init; condition; update) { }
for (type item : container) { }  // range-based

Example

cpp
#include <iostream>
using namespace std;

int main() {
    int nums[] = {1, 2, 3, 4, 5};
    for (int n : nums) {
        cout << n << " ";
    }
    cout << endl;
    return 0;
}

Output

output1 2 3 4 5

Important Points

Common Mistakes

Interview Tip

🎯 Before your interview Know when a range-based for loop isn't enough — e.g. when you need the current index, a classic for loop is still the right tool.
Back to notes Read full lesson →