Updated March 16, 2023
Introduction to While Loop in C
With the advent of technology, computers have developed, which in turn brought the requirement for programming language. There were many programming languages which include both low-level language as well as high-level language. High-level languages are easier to use as they are easy to understand in comparison to low-level languages. C is one such high-level language that is used widely for programming purposes. However, there are many concepts that one needs to study and practice to understand basic concepts. In this article, we will discuss While Loop in C.
What is While Loop in C?
There are several conditional methods in C, such as the if-else method, if-else-if method, while method, do-while method, and several other methods too. Out of such different methods in C, one such method is the while loop method. We use this method to run a particular set of instructions or code if the condition satisfies. A while loop statement generally contains sets of instructions. As per the condition, one or multiple lines of code may execute if the expression is true. If the expression is not satisfied, then the code of instruction within the loop will not be executed. It gets executed when the expression gets satisfied.
Syntax of While Loop in C
Let us try to understand the basic syntax of the while loop in C.
While (condition which needs to be evaluated)
{
Instructions of code
Increment of the value;
}
Now, let us try to understand how this block of the statement actually runs.
- The Condition which needs to be Evaluated: The code within these brackets is used to provide conditions that need to be evaluated. If this condition of evaluation gets satisfied, then the instructions of the code get executed. A typical example may be to check if the variable x is less than 10.
- Instructions of Code: Here, we add those lines of code that need to be performed once the condition gets satisfied and the execution is inside the while loop. A typical example may be to print the value of the variable over which the loop is running.
- Increment of the Value: In this section, simply value is incremented. The value of the variable which is incremented is the variable using which the loop is executing.
Flow Diagram
Now, let us look at the flowchart.
Now, let us look at how while loop works in C.
How While Loop Works in C?
As explained earlier in the article, a while loop generally contains three sub-sections which are-
- The condition which needs to be Evaluated: The code within these brackets is used to provide conditions that need to be evaluated. If this condition of evaluation gets satisfied, then the instructions of the code get executed. A typical example may be to check if the variable x is less than 10.
- Instructions of Code: Here, we add those lines of code that need to be performed once the condition gets satisfied and the execution is inside the while loop. A typical example may be to print the value of the variable over which the loop is running.
- Increment of the Value: In this section, simply value is incremented. The value of the variable which is incremented is the variable using which the loop is executing.
Examples of While Loop in C
Let’s understand how to use the While Loop in C with some examples.
Example #1
Write a Program to loop a variable from 1 to 10.
Code:
#include<stdio.h>
int main () {
int x = 1; // initializes a variable with value 1
while (x < 10) { // condition which needs to be evaluated
// code of instructions which needs to be executed
x++; // incremental value
}
}
Now, copy the code and run it C environment. It will simply run the above code.
Example #2
Write a Program to Print Factorial of a 15 using While Loop.
Code:
#include<stdio.h>
int main () {
int i = 15, factorial = 1;
while (i >= 1){
factorial = factorial * i;
i--;
}
printf ("The factorial of the number entered by the user is %d", factorial);
return 0;
}
Output:
Description: The factorial of the number entered by the user is 1307674368000.
In the above example, we declare variable I with value 15, whose factorial we need to find. Now, we will iterate a while loop over variable i. A variable factorial will use to store the factorial value.
Now, in the next section, we will use this example to receive the number whose factorial we need to find from the user. Now, it will be more dynamic where factorial of any number can be found based on user value.
Example #3
Now let us modify the above code to receive the number as an input from a user and print its factorial.
Code:
#include<stdio.h>
int main () {
int numFromUser, i, factorial = 1;
printf ("Enter the number\n");
scanf ("%d", &numFromUser);
i = numFromUser;
while (i >= 1){
factorial = factorial * i;
i--;
}
printf ("The factorial of the number entered by the user is %d", factorial);
return 0;
}
Explanation of the Code
- Variable numFromUser stores the value from the user.
- Variable factorial holds the factorial value.
Output:
Conclusion
C is a programming language where there are lots of concepts that one needs to study. While Loop is one of those. These conditional statements basically execute the code to check whether the expression satisfies the condition. Then, based on the expression evaluation, it executes the code. A conditional statement is widely used in any programming language to various logical programming expressions.
Recommended Articles
This is a guide to While Loop in C. Here we discuss what is While Loop in C, Flow Diagram, How While Loop works in C, and examples of While Loop in C. You can also go through our other suggested articles–