C while
last modified January 9, 2023
C while tutorial shows how to create loops in C with while statement.
The while statement
The while statement is used to create a while loop. A while
loop is a control flow statement that allows code to be executed repeatedly
based on a given boolean condition.
This is the general form of the while loop:
while (expression) {
statement(s);
}
The while keyword executes the statements inside the block enclosed
by the curly brackets. The statements are executed each time the expression is
evaluated to true.
C while example
The following example uses a while statement to calculate a sum.
#include <stdio.h>
int main() {
int i = 0;
int sum = 0;
while (i < 10) {
sum += i;
i++;
}
printf("%d\n", sum);
return 0;
}
We calculate the sum of 1..9 numbers.
The while loop has three parts: initialization, testing and updating. Each execution of the statement is called a cycle.
int i = 0;
We initiate the i variable. It is used as a counter.
while (i < 10) {
...
}
The expression inside the round brackets following the while
keyword is the second phase: the testing. The statements in the body are
executed until the expression is evaluated to false.
i++;
This is the last, third phase of the while loop: the updating. We
increment the counter. Note that improper handling of the while
loops may lead to endless cycles.
$ ./simple 45
C while - calculate factorial
The factorial of a positive integer n, denoted by n!,
is the product of all positive integers less than or equal to n.
n! = n * (n-1) * (n-2) * ... * 1
This is the formula to calculate the factorial.
#include <stdio.h>
int main() {
int i = 10;
int factorial = 1;
while (i > 1) {
factorial *= i;
i--;
}
printf("%d\n", factorial);
return 0;
}
In the example, we use the while loop to calculate the 10! factorial.
$ ./factorial 3628800
C while - endless loop
The while (1) creates an endless loop. In order to terminate the
loop, we use the break statement.
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
srand(time(NULL));
while (1) {
int r = rand() % 30;
printf("%d ", r);
if (r == 22) {
break;
}
}
printf("\n");
return 0;
}
The example calculates a random value between 0..29. If it equals to 22, the
loop is finished with the break statement.
$ ./random 21 6 0 4 20 26 14 6 0 29 12 15 17 2 15 24 12 17 25 29 6 10 14 22
C while - loop over array
The while statement can be used to traverse over an array.
#include <stdio.h>
int main() {
int vals[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int i, sum = 0;
size_t len = sizeof vals / sizeof vals[0];
while (i <= len) {
sum += vals[i];
i++;
}
printf("The sum is: %d\n", sum);
return 0;
}
In the example, we have an array of integers. We go over the array with the
while statement and calculate the sum of the values.
$ ./loop_array The sum is: 55
C do while example
The do while statement is a specific form of a while statement, where the block is executed before the condition. So the block is always executed at least once.
#include <stdio.h>
int main() {
int val, sum = 0;
do {
printf("Enter a number: ");
scanf("%d", &val);
sum += val;
} while(val != 0);
printf("The sum is: %d\n", sum);
return 0;
}
The example asks the user to repeatedly enter a number. It calculates the sum of all those values. It terminates the loop when the user enters zero.
$ ./do_while Enter a number: 2 Enter a number: 3 Enter a number: 4 Enter a number: 5 Enter a number: 0 The sum is: 14
In this article, we have covered the while statement in C.