While Loop in C

While Loop in c programming, a step-by-step guide with an easy explanation and detailed source code. Best Explanation of a while loop in C Programming Language. An expression containing a while loop returns either 0 or 1 depending upon whether the expression results in true or false.

While loop in c is the most basic loop in C programming. while loop has one control condition and executes as long the condition is true. Loops are used to perform the same task again and again. Execute the same logic multiple times. This helps in solving many complicated mathematical problems of Matrix and reduces the work of writing code since the same code can be executed multiple times.

The 3 important things to write any loops apart from business are

  1. Initialization
  2. Condition
  3. Increment/Decrement

If you miss anything while writing a loop, it could be an infinite loop. Understand what is while loop, how while loops works and how to write them with the simplest example. While Loop in C programming has never been so easy.

This blog demonstrates a while loop in C programming. To know more about C Programming and its history, please read this blog first, “C Programming Complete Guide“. You can watch the video over here which explains the blog in a simple video with VFX animation.

Syntax of while loop in C

The syntax of the while loop is:

while (condition)
      // Statements to be executed repeatedly 
      // Increment (++) Operation or Decrement (--) Operation

Types of Loops

  1. WHILE Loop
  2. DO WHILE Loop
  3. FOR Loop.

In this blog, we will focus more on the WHILE loop in c.

Write a program to Print printf statement 9 times using While Loop in C

Now suppose you want to print 1 to 9. What will you do? Will you write the printf statement 9 times? NO! It’s too much work. What you will do is you will write a printf statement one time, and you will execute it 9 times. Let’s open our source code again. What we will do is, We will put up the printf statement inside while.

Declaring Variables in C Program

Here, to start, we will create a program that will add numbers. Here we will create integers using the keyword int variables i.


int i;


Initialize variable

Now we put value 1 in i. Initialization is a process in C Programming where we initialize variables at the start of the program or business logic. Though we have used it to printf statements 9 times in C Programming.


int i = 1;


Program Logic to printf statement 9 times using WHILE loop in c

Let’s open our source code again. What we will do is, We will put up the printf statement inside while.

int i = 1;
while (i < 10)
printf("%d ", i);

If you want to copy-paste the code, please refer to our Newtum Github repository link over here.


1 2 3 4 5 6 7 8 9

In the while statement, we will write a condition that is i less than 10; remember, we have used this condition in IF and ELSE statements. Here we are telling our program to run the print value statement till 9. But the value of i is only 1. Here we have initialized the variable i equal to 1. This is called “initialization.

Next, we have given the condition, and after that, we have given the increment. Now, the only thing is the printf statement. Instead of printf, we can write any logic that we want, So What we understand is for Loop execution.

Three things are very important in the loop

  1. First is the initialization
  2. Second is Condition
  3. Third is Increment

Just try to understand the program. First Computer reaches the part of initialization that is i equal to 1. Next, it reaches the while condition. In a while, It checks whether i is less than 10, but currently, the value of i is 1, So 1 is definitely less than 10. The result is True. So it goes to the printf statement, and it prints the value of i, which is 1. In the next statement, it says, i equal to i plus 1.

So the value of i becomes 2. Now what happens is, It goes to the end of the bracket and again it jumps to the condition. In the condition, it checks whether i is less than 10 or not But the value of i is 2 So Again the condition is True Since the condition is True, The system enters the Loop again and it prints the value of i that is 2. Now it enters the increment code, In increment, the value of i becomes 3. Now it again enters the condition and in the condition, the system checks whether i is less than 10 or not.

But again the condition is true because 3 is less than 10. So What happens is, it again enters the loop. Now, suppose we have done the printing till 9. After printing the value of 9, the System goes to increment, and the value of i becomes 10. Now it again checks the while condition. Condition is, i less than 10 or Not. But 10 is definitely not less than 10, So the condition Breaks And the system skips the while Loop and goes to the next line. Here the while loop ends. The system reaches the end of the program.

How to view all topics on C Programming

Watch the full programming series on Amazon Prime free with Amazon Prime Membership.

Frequently Asked question about While Loop

What are the types of loops?

There are three main types of loops,
1. WHILE Loop,
2. DO WHILE Loop and
3. FOR Loop.

Why do we need loops in programming?

Let’s consider the situation when you want to write Hello, World! A hundred times. We can certainly not write printf() statements a hundred times. Almost all programming languages provide a concept called loop, which helps in executing one or more statements up to a desired number of times. All high-level programming languages provide various forms of loops, which can be used to execute one or more statements repeatedly.

About The Author