While loop in C Language with Example Programs

Spread the love
while-loop-in-c-programming-language-with-example-programs

Introduction:

We have looked at the Switch statement in our previous article, In today’s article, We are going to look at the Loops in programming and While loop in C Language. We also look at the step-by-step walk-through of the while loop, along with the Infinite loops in C Programming.

Loops in Programming:

There will be times when you want to repeat a piece of code multiple times.

For example, if You want to print all even numbers from 1 to 1000, You can go ahead and write 500 printf statements. But that is not ideal. In such cases, We can use the Loop.

Loop:

As the name suggests, Repeat the code block until you met some condition.

In the above case, You might maintain a counter and once the counter reaches 1000, You will stop the loop.

There are three loops in C Programming language.

  1. while loop
  2. do while loop
  3. for loop

We are going to discuss the while loop in this article.

While loop in C Language:

While loop is used to execute a set of statements repeatedly (in a loop). Until a condition is met.

Here is the syntax of the while loop

While loop Syntax:

How while loop works in C Language:

We have a Condition in the while loop, Which evaluates to either true or false

First, the while loop checks the condition if the condition is evaluated to true , Then the Code inside the while loop (Body of While loop) will be executed.

After executing the statements in the while loop, while checks the condition again, If the condition is evaluated true, then it again executes the statements inside the while body.

This process will continue until the condition becomes false.

Once the while condition becomes false, The While loop exit and control come out of the loop to execute the subsequent statements in the program.

📢 The Body of the while is executed until the condition becomes false

Flow Diagram of While loop in C language:

While-loop-in-c-language-with-example-program

Example to demonstrate the while loop in C:

Let’s write a C program to print the even numbers from 1 to 10 using the while loop.

save the program as the while-loop.c and run it using your compiler.

Program Output:

we used gcc compiler to compile the program.

Program Explanation: While Loop Step-by-Step walk-through:

To print the even numbers from 1 to 10, We take a variable i and assign it the value 1. Then we have our while condition which is i ≤ 10 ,

First Iteration:

At the start,

  • The i will be 1, So condition i 10 evaluated as true
  • Body of while is executed
  • Which checks if i is an even number using the modulus operator and prints i variable if it even, Our i value is 1, So it is not an even number. So the number won’t be printed on the console.
  • After that, we are increasing the i by 1, So our i variable becomes 2

Output so far : no-output-so-far

Second Iteration:

  • As while is a loop, It will again go back to start and checks the condition( i 10 )
  • Now i is 2, So condition i 10 evaluated as true
  • The body of while is executed
  • Which checks if i is an even number using the modulus operator and prints i variable if it even, Our i value is 2, So it is an even number. So the number 2 will be printed on the console.
  • After that, we are increasing the i by 1, So i variable becomes 3

Output so far: 2

Third Iteration:

  • we will again go back to the condition and checks the condition( i 10 )
  • Now i is 3, So condition 3 10 evaluated as True
  • The body of while is executed
  • Which checks if i is an even number using the modulus operator and prints i variable if it even, Our i value is 3, So it is not an even number. So the number is not printed on the console.
  • After that, we are increasing the i by 1, So i variable becomes 4

Output so far: 2

Fourth Iteration:

  • While will again go back to start and checks the condition( i 10 )
  • Now i is 4, So condition 4 10 is evaluated as True
  • The body of while is executed.
  • Which checks if i is an even number using the modulus and prints i variable if it even, Our i value is 4, So it is an even number. So the number 4 will be printed on the console.
  • After that, we are increasing the i by 1, So i variable becomes 5

Output so far: 2 4

Fifth Iteration:

  • While will again go back to start and checks the condition( i 10 )
  • Now i is 5, So condition 5 10 is evaluated as True
  • The body of while is executed
  • Here we are checking if i is an even number using the modulus operator and printing the value of i, if it even, Our present i value is 5, So it is not an even number. So the number 5 won’t be printed on the console.
  • After that, we are increasing the i by 1, So the i variable becomes 6

Output so far: 2 4

Sixth Iteration:

  • While will again go back to start and checks the condition( i 10 )
  • Now i is 6, So condition 6 10 is evaluated as True
  • The body of while is executed.
  • Which checks if i is an even number using the modulus and prints i variable if it even, Our i value is 6, So it is an even number. So the number 6 will be printed on the console.
  • After that, we are increasing the i by 1, So i variable becomes 7

Output so far: 2 4 6

Seventh Iteration:

  • While will again go back to start and checks the condition( i 10 )
  • Now i is 7, So condition 7 10 is evaluated as True
  • The body of while is executed
  • Here we are checking if i is an even number using the modulus operator and printing the value of i, if it even, Our present i value is 7, So it is not an even number. So the number 7 won’t be printed on the console.
  • After that, we are increasing the i by 1, So the i variable becomes 8

Output so far: 2 4 6

Eighth Iteration:

  • While will again go back to start and checks the condition( i 10 )
  • Now i is 8, So condition 8 10 is evaluated as True
  • The body of while is executed.
  • Which checks if i is an even number using the modulus and prints i variable if it even, Our i value is 8, So it is an even number. So the number 8 will be printed on the console.
  • After that, we are increasing the i by 1, So i variable becomes 9

Output so far: 2 4 6 8

Ninth Iteration:

  • While will again go back to start and checks the condition( i 10 )
  • Now i is 9, So while condition 9 10 is evaluated as True
  • The body of while is executed
  • Here we are checking if i is an even number using the modulus operator and printing the value of i, if it even, Our present i value is 9, So it is not an even number. So the number 9 won’t be printed on the console.
  • After that, we are increasing the i by 1, So the i variable becomes 10

Output so far: 2 4 6 8

Tenth Iteration:

  • While will again go back to start and checks the condition( i 10 )
  • Now i is 10, So condition 10 10 is evaluated as True
  • The body of while is executed.
  • Which checks if i is an even number using the modulus and prints i variable if it even, Our i value is 10, So it is an even number. So the number 10 will be printed on the console.
  • After that, we are increasing the i by 1, So i variable becomes 11

Output so far: 2 4 6 8 10

Eleventh Iteration:

  • While will again go back to start and checks the condition( i 10 )
  • Now i is 11, So condition 11 10 is evaluated as False

Now the whileloop will stop the iteration and Control comes out of the while loop.

Final Output : 2 4 6 8 10

📢 As you can see the while loop only exits if the condition becomes false

Example 2: While Loop to Calculate the sum up to given Number:

Let’s write another program to calculate the sum of all numbers from 1 to the user-provided number.

We are going to use the while loop to iterate over the numbers from 1 to user-given number.

Paste the above code and save it.

Program Output:

We are using the GCC compiler to compile the program. you can use your favorite compiler.

Here are the instructions to compile the program in Linux.

While loop in C language Example Program Explanation:

In the above program, We have asked a number from the user and stored in the num variable and then we used the while loop to iterate from 1 to the num.

We also took a counter variable, which is a control variable for our while loop, We increment this variable while iterating from 1 to num

We also have the sum variable, The default value of the sum is zero.

At each iteration, We add the value of the counter to the sum and we also increment the counter to next number (i.e increment by 1).

So the sum will become 0, 1, 3, 6, so on. Depending on the num the while loop stops.

For example, If the user given 4 as the num, Then

  • In the first iteration, counter = 1 and the sum becomes 1 ( sum = sum + counter which is sum = 0+1).
  • Second Iteration, counter = 2 and the sum becomes 3 ( sum = 1+2)
  • Third Iteration, counter = 3 and the sum becomes 6 ( sum = 3+3)
  • fourth Iteration, counter = 4 and the sum becomes 10 ( sum = 6+4)
  • Fifth Iteration, counter = 5, which makes the while condition counter<= num i.e 54 to fail. So while loop will be exited and control comes out of the while loop.

Infinite Loops in C Language:

Any while loop can become an Infinite loop if the while condition never becomes False. So the while loop will run forever.

💡 While loop has a counter, In the above case it is our variable counter. We need to make sure to increment or decrement the counter variable counter to meet our while condition. In the above case, we incremented the counter.

If you forgot to increment or decrement the counter variable, Then whilecondition won’t become False, Which means while always returns True. This causes the body of the whileloop to execute forever. This phenomenon is called Infinite Loop.

Here is an example program of an Infinite loop:

Infinite Loop Example in C Programming:

Infinite loop Program Output:

We can create Infinite loops by not incrementing or decrementing the while control variable (i.e counter)

Here we have not incremented the counter value inside the while loop, So the counter value remained the 1 forever. So the while condition 1<=10 always returns True. So our while loop returns True forever.

If you execute the above program, You will see the Counter value: 1 prints continuously on the console.

Note: Above program runs continuously so to kill the program press the Control + c (If you are using a Linux terminal) or Close the output window (if you are using the Windows PC)

Avoiding the Infinite Loops in C Language:

To solve the above infinite loop problem, We need to increment the counter variable inside the loop.

Here is the modified code, which prevents the infinite loop.

Program Output:

As you can see from the above code, we incremented the counter value inside the while loop. At each iteration, we increment the counter value by 1, which takes the counter value closer to 10 and finally makes the while condition counter <= 10 to False. and the while loop will exit and program control comes out of the while loop.

Where to use Infinite loops:

There are a few cases where you want to run your program forever.

For example, a Web Server can run forever to serve the requests from clients. In such cases, The web server needs to be online and running forever. In such cases, we use Infinite loops.

Conclusion:

We have discussed about the Loops in C programming language and specifically, we discussed about the While loop in C Language with example programs. We even looked at the step-by-step walk-through of the while loop. Finally, we looked at the Infinite loops and how infinite loops occur, and how we can avoid them.

Tutorials Index:

Pattern Programs:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

4 Responses

  1. September 19, 2022

    […] While Loop in C Language […]

  2. September 23, 2022

    […] have discussed the while loop in our earlier article, In today’s article, We will look at the for loop in C language with […]

  3. September 25, 2022

    […] have discussed about the while loop and for loop in c language in our earlier articles, In today’s article, We are going to look […]

  4. October 2, 2022

    […] while loop […]

Leave a Reply