Control Flow
Loops: while, do-while and for
Repeat work with the three C loops, choose the right one, and write the classic digit, factorial, prime and Fibonacci programs.
A loop repeats a block of code while a condition stays true. C has three loops. They can all do the same job; each one simply fits a different situation.
while
A while loop checks its condition before each pass. Use it when you do not know in advance how many times to repeat.
#include <stdio.h>
int main(void) {
int n, sum = 0, count = 0;
printf("Enter a number: ");
scanf("%d", &n);
while (n > 0) {
sum += n % 10; // take the last digit
n /= 10; // drop the last digit
count++;
}
printf("Digits: %d, Sum of digits: %d\n", count, sum);
return 0;
}Enter a number: 40213
Digits: 5, Sum of digits: 10do...while
A do-while loop checks its condition after each pass, so the body always runs at least once. That makes it perfect for menus:
#include <stdio.h>
int main(void) {
int choice;
do {
printf("1) Say hi 2) Say bye 3) Quit : ");
scanf("%d", &choice);
if (choice == 1) printf("Hi!\n");
else if (choice == 2) printf("Bye!\n");
} while (choice != 3);
printf("Menu closed.\n");
return 0;
}1) Say hi 2) Say bye 3) Quit : 1 Hi! 1) Say hi 2) Say bye 3) Quit : 2 Bye! 1) Say hi 2) Say bye 3) Quit : 3 Menu closed.
for
A for loop packs the three parts of counting into one line: initialise; condition; update.
#include <stdio.h>
int main(void) {
for (int i = 1; i <= 5; i++) {
printf("5 x %d = %d\n", i, 5 * i);
}
return 0;
}5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25
The order is: initialise once → check condition → run body → update → check condition → … until the condition is false.
for loop runs. After the body and i++, control jumps back to the condition.Choosing a loop
| Loop | Checks condition | Runs at least once? | Best for |
|---|---|---|---|
for |
before each pass | no | counting a known number of times |
while |
before each pass | no | repeating until something happens |
do-while |
after each pass | yes | menus, input validation |
Classic loop programs
Factorial
#include <stdio.h>
int main(void) {
int n;
long long fact = 1; // factorials grow fast, so use a big type
printf("n = ");
scanf("%d", &n);
for (int i = 2; i <= n; i++)
fact *= i;
printf("%d! = %lld\n", n, fact);
return 0;
}n = 10
10! = 3628800Prime check
A number is prime if no number from 2 to √n divides it. Checking only up to √n makes the loop much faster:
#include <stdio.h>
int main(void) {
int n, isPrime = 1;
printf("n = ");
scanf("%d", &n);
if (n < 2) isPrime = 0;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) { isPrime = 0; break; }
}
printf("%d is %s\n", n, isPrime ? "prime" : "not prime");
return 0;
}n = 97
97 is primeFibonacci series
#include <stdio.h>
int main(void) {
int n, a = 0, b = 1;
printf("How many terms? ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("%d ", a);
int next = a + b;
a = b;
b = next;
}
printf("\n");
return 0;
}How many terms? 10
0 1 1 2 3 5 8 13 21 34Reverse a number and check palindrome
#include <stdio.h>
int main(void) {
int n, original, reversed = 0;
printf("n = ");
scanf("%d", &n);
original = n;
while (n > 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
printf("Reversed: %d\n", reversed);
printf("%d %s a palindrome\n", original, original == reversed ? "is" : "is not");
return 0;
}n = 12321
Reversed: 12321
12321 is a palindromeTest yourself
4 questions · pick an answer to check it instantly
- 1
How many times does this print
Hi?Cint i = 10; do { printf("Hi"); } while (i < 5);Ado-whileloop runs its body once before checking the condition. - 2
How many times does
for (int i = 0; i <= 10; i += 2)run?itakes the values 0, 2, 4, 6, 8 and 10: six passes. - 3
Why does the prime check loop use
i * i <= n?Ifn = a × band both were above √n, their product would exceedn. So a factor ≤ √n must exist ifnis composite. - 4
Which loop is best for a menu that must appear at least once?
do-whilealways executes its body once before testing the condition.