Loop patterns, sums and star patterns
A loop on its own just repeats. Most real loops build up an answer one step at a time: a total, a count, a product. These are the patterns you'll meet in class and in exams; run each one and change the numbers.
Iteration and the sum of 1 to 10
Each pass through a loop's body is an iteration. To add numbers up, keep a running total in an accumulator: start it at 0 before the loop, add to it inside, and use it after:
#include <iostream>
using namespace std;
int main() {
int sum = 0; // 1. start the total at 0
for (int i = 1; i <= 10; i++) {
sum += i; // 2. add this number to the total
cout << "iteration " << i << ": total is " << sum << endl;
}
cout << "Sum of 1 to 10 = " << sum << endl; // 3. use it after the loop
return 0;
}| i | sum before | sum after |
|---|---|---|
| 1 | 0 | 1 |
| 2 | 1 | 3 |
| 3 | 3 | 6 |
| ... | ... | ... |
| 10 | 45 | 55 |
When i becomes 11, i <= 10 is false and the loop stops. Forgetting = 0 on sum leaves it holding a random leftover value.
Counting down: 10 to 1 in reverse
Start high, continue while i >= 1, and subtract with i--. Adding while counting down gives the same 55:
#include <iostream>
using namespace std;
int main() {
int sum = 0;
for (int i = 10; i >= 1; i--) {
cout << i << " ";
sum += i;
}
cout << endl << "Sum of 10 down to 1 = " << sum << endl;
int n = 5;
while (n >= 1) { // the same countdown with while
cout << n << "... ";
n--;
}
cout << "lift off!" << endl;
return 0;
}Conditional sums, counting and factorials
#include <iostream>
using namespace std;
int main() {
int evens = 0, odds = 0, sevens = 0;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) evens += i;
else odds += i;
}
for (int i = 1; i <= 100; i++) {
if (i % 7 == 0) sevens++; // a counter adds 1 each time
}
int fact = 1; // a product starts at 1, not 0
for (int i = 1; i <= 10; i++) fact *= i;
cout << "Even sum: " << evens << ", odd sum: " << odds << endl;
cout << sevens << " numbers from 1 to 100 divide by 7" << endl;
cout << "10! = " << fact << endl;
return 0;
}Times tables, array totals and digits
#include <iostream>
using namespace std;
int main() {
int n = 7;
for (int i = 1; i <= 10; i++) {
cout << n << " x " << i << " = " << n * i << endl;
}
int marks[5] = {72, 85, 90, 64, 88};
int total = 0, highest = marks[0];
for (int i = 0; i < 5; i++) {
total += marks[i];
if (marks[i] > highest) highest = marks[i];
}
cout << "Total: " << total << ", highest: " << highest << endl;
int num = 4096, digitSum = 0, reversed = 0;
while (num > 0) {
int digit = num % 10; // the last digit
digitSum += digit;
reversed = reversed * 10 + digit;
num /= 10; // drop the last digit
}
cout << "Digit sum: " << digitSum << ", reversed: " << reversed << endl;
return 0;
}Patterns with nested loops
The outer loop picks the row; the inner loop prints that row. A triangle row has row stars; a pyramid row has n - row spaces then 2 * row - 1 stars; Floyd's triangle keeps a counter outside both loops:
#include <iostream>
using namespace std;
int main() {
int n = 5;
for (int row = 1; row <= n; row++) {
for (int col = 1; col <= row; col++) cout << "* ";
cout << endl;
}
for (int row = 1; row <= n; row++) {
for (int s = 1; s <= n - row; s++) cout << " ";
for (int k = 1; k <= 2 * row - 1; k++) cout << "*";
cout << endl;
}
int num = 1;
for (int row = 1; row <= 4; row++) {
for (int col = 1; col <= row; col++) {
cout << num << " ";
num++;
}
cout << endl;
}
return 0;
}Your turn: sum of 1 to n
This example reads n with cin; its input is 100:
#include <iostream>
using namespace std;
int main() {
int n, sum = 0;
cout << "Add up 1 to which number? ";
cin >> n;
for (int i = 1; i <= n; i++) sum += i;
cout << endl << "1 + 2 + ... + " << n << " = " << sum << endl;
cout << "Formula check: " << n * (n + 1) / 2 << endl;
return 0;
}