cpp / beginner
Snippet
Iterating with For Loops
A for loop repeats a block of code a specific number of times. It is ideal when you know exactly how many iterations are needed.
snippet.cpp
1
2
3
for (int i = 0; i < 5; ++i) {std::cout << "Count: " << i << std::endl;}
Breakdown
1
for (int i = 0; i < 5; ++i) {
Starts the loop: initializes 'i' to 0, checks if 'i' is less than 5, and increments 'i' after each step.
2
std::cout << "Count: " << i << std::endl;
The body of the loop which prints the current value of 'i' to the console.