C Programming for Beginners : The While Loop

Suraj Das
3 min readDec 11, 2021

--

Photo by Tine Ivanič on Unsplash

What are loops in Programming ?

Sometimes you have to repeat some tasks. For example, printing serial numbers. Here, you can type the numbers manually but this can make you feel like dying 😫 So instead we use loops.

You can observe that here you are doing the same task over and over again. You are first printing a number and incrementing it. You are repeating this task for a certain period (like for the number of entries.)

While Loop in C

We can do the “serial number thing” by using the While loop in C.

First know the number up to which we will be printing the serial (numbers of data entries)

We took 5 in this case.

Now we generate our first serial number.

It’ll of course be 1

We generate a While loop.

We describe the condition within those simple brackets.

The code inside the loop will be iterating through itself over and over till the condition remains true.

Now we already know what is the task that we have to repeat.

print i

add 1 to i

print i

add 1 to i

print i

.

.

.

Code for this is :

This process should stop when we print 5 (as there are 5 entries.) So it should not print 6.

So we put this as the condition.

So the code is basically saying to print i and add 1 to it. At the same time it checks if i is not 6.

When i becomes 6, the condition becomes false and we break out of the loop.

Complete Code :

1
2
3
4
5

We can do much more interesting things with the while loop. And also we can generate an endless loop by specifying a condition which remains true no matter what. For example 10 is equal to 10 no matter what you say.

Now you are stuck in an endless loop 😂
Kill your terminal to get out of it ✌️

Hope you enjoyed this blog

Have any doubt ?
DM me on Instagram

Peace ✌️

Index of C Programming lessons :

  1. Getting Started within 5 minutes
  2. Learning Fundamentals Made Easy
  3. Circumference and Area of Circle
  4. scanf() vs fgets()
  5. Hypotenuse Calculator
  6. Learn by Building a Calculator
  7. Functions
  8. More on Functions
  9. Length of an Array
  10. String Functions
  11. The While Loop

--

--