While Loop in C
What is "While Loop in C"
A while loop in C is a control flow statement that repeatedly executes a block of code as long as the specified condition is true.
The syntax for a while loop in C is as follows:
while (condition) {
// code to be executed
}
while
keyword is used to create a while loop.The condition is an expression that is evaluated before each iteration of the loop.
The code inside the curly braces
{}
is executed as long as the condition is true.Once the condition becomes false, the loop is terminated and control is transferred to the next statement following the loop.
For example:
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
This will print the numbers 0 through 4.
Where should we use "while Loop in C"
A while loop in C should be used when you want to repeatedly execute a block of code as long as a certain condition is true.
Some examples of when you might use a while loop include:
When you want to repeatedly prompt the user for input until they enter a valid value.
When you want to process a large amount of data and need to keep track of the current position within the data.
When you want to repeatedly perform a task until a certain condition is met, like waiting for a flag to be set or a button to be pressed.
When you want to repeatedly perform a task a certain number of times.
It's important to keep in mind that if the condition never becomes false, the loop will continue to execute indefinitely, causing an infinite loop. So, it's very important to include a way to exit from the loop by changing the condition in the body of the loop, otherwise the program will hang.
Examples of using "while loop" in C
Example 1
Below is an example to repeatedly print the current value of the variable num as long as it is less than 5.
#include <stdio.h>
int main()
{
int num = 0;
while (num < 5)
{
printf("The current value of num is: %d\n", num);
num++;
}
return 0;
}
Output:
Explanation
This program uses a while
loop to repeatedly print the current value of the variable num as long as it is less than 5.
Here's a breakdown of what's happening in the program:
The variable
num
is initialized to 0.The
while
loop starts with the conditionnum < 5
. This condition is evaluated before each iteration of the loop.As long as the condition is
true
, the code inside the loop is executed. In this case, the current value ofnum
is printed to the screen.After the code inside the loop is executed, the value of
num
is incremented by 1.The condition
num < 5
is evaluated again. If it is stilltrue
, the loop continues to execute again. If it isfalse
, the loop is terminated.
The output of the program will be:
The current value of num is: 0
The current value of num is: 1
The current value of num is: 2
The current value of num is: 3
The current value of num is: 4
Example 2
Here is another example of a program, that uses a while
loop to find the sum of the numbers from 1 to 10.
#include <stdio.h>
int main()
{
int sum = 0, i = 1;
while (i <= 10)
{
sum += i;
i++;
}
printf("The sum of the numbers from 1 to 10 is: %d\n", sum);
return 0;
}
Output:
Explanation
This program uses a while loop to find the sum of the numbers from 1 to 10.
Here's a breakdown of what's happening in the program:
The variables
sum
andi
are initialized to 0 and 1 respectively.The while loop starts with the condition
i <= 10
. This condition is evaluated before each iteration of the loop.As long as the condition is
true
, the code inside the loop is executed. In this case, the current value ofi
is added to the variable sum.After the code inside the loop is executed, the value of
i
is incremented by 1.The condition
i <= 10
is evaluated again. If it is stilltrue
, the loop continues to execute again. If it isfalse
, the loop is terminated.After the
while
loop, the final value of the variablesum
is printed to the screen.
The output of the program will be:
The sum of the numbers from 1 to 10 is: 55
Example 3
Example of a program that uses a while
loop to print the digits of a number entered by the user in reverse order.
#include <stdio.h>
int main()
{
int n = 12345;
while (n != 0)
{
printf("%d ", n % 10);
n = n / 10;
}
return 0;
}
Output:
Explanation
Here's a breakdown of what's happening in the program:
The variable
n
is initialized to 0.The user is prompted to enter a number which is stored in the variable
n
using thescanf
function.The
while
loop starts with the conditionn != 0
. This condition is evaluated before each iteration of the loop.As long as the condition is
true
, the code inside the loop is executed. In this case, the last digit of the number is printed using the expressionn % 10
and then the number is divided by 10 usingn = n / 10
The condition
n != 0
is evaluated again. If it is stilltrue
, the loop continues to execute again. If it isfalse
, the loop is terminated.
The output of the program is:
5 4 3 2 1
When using a while loop, it is important to make sure that the loop will eventually exit. Otherwise, the loop will continue to execute indefinitely, causing an infinite loop. To avoid infinite loops, make sure that the condition being evaluated in the while loop will eventually become false by including a way to change the condition in the body of the loop.