EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

While Loop in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » While Loop in C

While Loops in C

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 as a programming purpose. There are lots of a concept which one needs to study and practice in order 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 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 while loop method. In this method, we use it to run a particular set of instructions or code if 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. In case of expression is not satisfied then the code of instruction within the loop will not executed. It gets executed when the expression gets satisfied.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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, are used to provide conditions which need to be evaluated. If this condition of evaluation gets satisfied then the instructions of 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 which 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 loop is executing.

Flow Diagram

Now, let us look at the flowchart.

while-loop-in-CDone

Now, let us look at how while loop works in C.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

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, are used to provide conditions which need to be evaluated. If this condition of evaluation gets satisfied then the instructions of 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 which 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 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;

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.

Output:

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.

Input:

Enter the number: 5

Output:

The factorial of the number entered by the user is 120.

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 condition 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–

  1. While Loop in Java
  2. C# While Loop
  3. Patterns in C Programming
  4. Best C Compilers

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More