While Loop in C

While loop in c programming, step by step guide with an easy explanation and detailed source code.

Introduction: While Loop in C

Best Explanation of a while loop in C Programming Language. An expression containing a while loop returns either 0 or 1 depending upon whether expression results true or false.

while loop in c is a 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. To 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 it’s history please read this blog first “C Programming Complete Guide 2020

You can watch the video over here which explains the blog in simple video with VFX animation.

Syntax of while loop:

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 and
  3. FOR Loop.

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

Example: write the printf statement 9 times 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.

main()
{

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 program or business logic. Though we have used it to printf statement 9 times in C Programming.

main()
{

int i = 1;

}

Business 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.

main()
{
int i = 1;
while (i < 10)
{
printf("%d ", i);
i++;
}
}

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

Output:

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 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 the 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, that is 1. In the next statement it says, i equals 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 system skips the while Loop and goes to the next line.

Here the while loop ends. The system reaches the end of the program.

[adrotate banner=”10″]

How to view all topics on C Programming

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

Amazon Prime link for the USA: https://www.amazon.com/dp/B07WFYJW6G

Amazon Prime link for the UK: https://www.amazon.co.uk/dp/B07WFYQZ1G


For Other Countries: To watch C Programming Content in another country, please visit the URL: https://newtum.com/course-details/c-programming-online

Frequently Asked question about While Loop

What are the types of loops?

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

Why 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 the 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.

Facebook Comments
(Visited 52 times, 1 visits today)

Leave A Comment

Your email address will not be published. Required fields are marked *