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. This lesson collects the patterns you'll meet in class and in exams; run each one and change the numbers.
Iteration: one trip round the loop
Each pass through a loop's body is an iteration. Tracing the loop below: i is 1, 2, then 3, and when it reaches 4 the condition i <= 3 is false, so the loop stops.
#include <stdio.h>
int main() {
for (int i = 1; i <= 3; i++) {
printf("Iteration number %d\n", i);
}
printf("Loop finished\n");
return 0;
}Adding up: the sum of 1 to 10
An accumulator holds the running total: start it at 0 before the loop, add to it inside, use it after:
#include <stdio.h>
int main() {
int sum = 0; /* 1. start the total at 0 */
for (int i = 1; i <= 10; i++) {
sum = sum + i; /* 2. add this number (also: sum += i) */
printf("after adding %d, the total is %d\n", i, sum);
}
printf("Sum of 1 to 10 = %d\n", sum); /* 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 |
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 <stdio.h>
int main() {
int sum = 0;
for (int i = 10; i >= 1; i--) {
printf("%d ", i);
sum += i;
}
printf("\nSum of 10 down to 1 = %d\n", sum);
int n = 5;
while (n >= 1) { /* the same countdown with while */
printf("%d... ", n);
n--;
}
printf("lift off!\n");
return 0;
}Conditional sums, counting and factorials
#include <stdio.h>
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;
}
printf("Even sum: %d, odd sum: %d\n", evens, odds);
printf("%d numbers from 1 to 100 divide by 7\n", sevens);
printf("10! = %d\n", fact);
return 0;
}Times tables and array totals
#include <stdio.h>
int main() {
int n = 7;
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", n, i, n * i);
}
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];
}
printf("Total: %d, highest: %d\n", total, highest);
printf("Average: %.1f\n", total / 5.0);
return 0;
}total / 5 would be integer division (79); dividing by 5.0 keeps the decimals.
Digit sum and reversing a number
n % 10 is the last digit; n / 10 drops it:
#include <stdio.h>
int main() {
int n = 4096, digitSum = 0, reversed = 0;
while (n > 0) {
int digit = n % 10;
digitSum += digit;
reversed = reversed * 10 + digit;
n = n / 10;
}
printf("Digit sum: %d\n", digitSum);
printf("Reversed: %d\n", reversed);
return 0;
}Patterns with nested loops
The outer loop picks the row; the inner loop prints that row. Row row of a triangle has row stars; a pyramid row has n - row spaces then 2 * row - 1 stars:
#include <stdio.h>
int main() {
int n = 5;
for (int row = 1; row <= n; row++) {
for (int col = 1; col <= row; col++) {
printf("* ");
}
printf("\n");
}
for (int row = 1; row <= n; row++) {
for (int s = 1; s <= n - row; s++) printf(" ");
for (int k = 1; k <= 2 * row - 1; k++) printf("*");
printf("\n");
}
int num = 1; /* Floyd's triangle */
for (int row = 1; row <= 4; row++) {
for (int col = 1; col <= row; col++) {
printf("%d ", num);
num++;
}
printf("\n");
}
return 0;
}Your turn: sum of 1 to n
This example reads n with scanf; its input is 100:
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Add up 1 to which number? ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
sum += i;
}
printf("\n1 + 2 + ... + %d = %d\n", n, sum);
printf("Formula check: %d\n", n * (n + 1) / 2);
return 0;
}