C Programming
In this tutorial we will learn about while loop in C programming language.
When we want to perform a certain set of tasks repeatedly like printing "Hello World" for 100,000 times, we can accomplish this mammoth task in two ways.
The naive approach:
We can either write printf("Hello World\n");
statement for 100,000 times.
The smart approach:
Or, we can take help of while loop which will execute the printf("Hello World\n");
code 100,000 times for us.
Syntax of a while loop.
while (condition) {
//while block code...
}
We use the while
keyword to create a while loop. And the code inside the while-block keeps executing as long as the condition is satisfied.
If the condition is not satisfied then we ignore the while loop.
In the following example we will print "Hello World" 10 times using the while loop.
#include <stdio.h>
int main(void)
{
//variable
int count = 1;
//loop
while (count <= 10) {
printf("Hello World\n");
//update
count++;
}
printf("End of code\n");
return 0;
}
Output
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
End of code
So, in the above code we have a variable count
which is initially set to 1. We are using this to loop 10 times.
Next we have the while
loop. The condition in the above code is count <= 10
which means we will execute the code inside the while loop as long as count is less than or equal to 10. So, when the value of count will become greater than 10 then we will jump out of the while loop.
Inside the while loop we have the printf
statement which prints "Hello World".
Then we are increasing the value of count
by 1 using the increment ++
operator.
On reaching the end of the while-block we recheck the condition and if it is still valid we rerun the loop otherwise, we come out of the loop.
In the following example we will print multiplication table of 2 using the while loop.
#include <stdio.h>
int main(void)
{
//variable
int product, count = 1;
//loop
while (count <= 10) {
product = 2 * count;
printf("2 x %d = %d\n", count, product);
//update
count++;
}
printf("End of code\n");
return 0;
}
Output
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
End of code
In the above code we have a count
variable which is initially set to 1 and will update till 10. We also have a product
variable to help us in storing the multiplication result.
The condition for the while
loop is count <= 10
so, the loop will execute as long as count is less than or equal to 10. Once the value of count becomes greater than 10, we will jump out of the loop.
Inside the loop we first find the product of 2 * count
which gives us 2, 4, 6 and so on.
Then we have the printf
statement which prints the product.
Finally we update the value of count by 1 using the increment ++
operator.
In the following example the while loop will keep printing the message "Hello World" till the program runs out of memory. This is because the condition is always satisfied so, we don't jump out of the loop.
#include <stdio.h>
int main(void)
{
//variable
int count = 1;
//loop
while (count > 0) {
printf("Hello World\n");
//update
count++;
}
printf("End of code\n");
return 0;
}
ADVERTISEMENT