cpp / beginner
Snippet
For Loops for Repetition
A for loop repeats a block of code a specific number of times. It has three parts: initialization (int i = 0), condition (i < 5), and increment (i++). The loop runs as long as the condition is true.
snippet.cpp
1
2
3
4
5
6
7
#include <iostream>int main() {for (int i = 0; i < 5; i++) {std::cout << "Iteration: " << i << std::endl;}return 0;}
Breakdown
1
#include <iostream>
Header file for input/output operations
2
for (int i = 0; i < 5; i++)
Loop header: start at 0, run while i < 5, increment each iteration
3
std::cout << "Iteration: " << i << std::endl;
Print the current iteration number with newline
4
return 0;
Indicate successful program completion