Foundations
Loops
Loops repeat a block while a counter changes.
Loops
loops.cpp
#include <iostream>
int main() {
int limit = ;
int total = 0;
for (int number = 1; number <= limit; number++) {
total += number;
}
std::cout << "limit=" << limit << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
int main() {
int limit = ;
int total = 0;
for (int number = 1; number <= limit; number++) {
total += number;
}
std::cout << "limit=" << limit << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
int main() {
int limit = ;
int total = 0;
for (int number = 1; number <= limit; number++) {
total += number;
}
std::cout << "limit=" << limit << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
for loop
A `for` loop can initialize a counter, test a condition, and update the counter after each pass.