cpp / beginner
Snippet
Repeating Actions with Loops
Loops let you repeat code multiple times without writing it again. The for-loop has three parts: start (i = 1), condition (i <= 5), and update (i++). It runs as long as the condition is true.
snippet.cpp
1
2
3
4
5
6
7
#include <iostream>int main() {for (int i = 1; i <= 5; i++) {std::cout << "Count: " << i << std::endl;}return 0;}
Breakdown
1
for (int i = 1; i <= 5; i++)
Loop header with initialization, condition, and increment
2
std::cout << "Count: " << i << std::endl;
Prints the current count value each iteration