While Loop in C Programming | C Tutorial

Let us implement While loop in C programming and understand how a while loop works in C programming with its algorithm, flowchart, example, nested while loop and explanation.

A While loop is a control flow statement that helps to repeatedly iterate a set of code based on a given Boolean condition. The while loop can be thought of as a repeating if – else statement.

 

While Loop Flowchart

While loop in C programming with syntax, flowchart, algorithm, example, output and explanation

This looping construct follows an entry condition system wherein the condition is tested before the program control moves into the inner blocks of code within the while block.

A While loop, therefore, verifies the condition before every iteration of the loop body.

While Loop Syntax

The program control first validates the condition and if it results to true, then the set of instructions within the while loop is executed.

This execution of a while loop is reiterated until the condition evaluates to false. Once, the condition becomes false, the program control exits out of the While loop and executes the other statements if any.

For the while condition to change at every iteration, it is important to provide a statement within the while loop which changes the condition variables.

While Loop Algorithm

For this purpose, we usually get to see an increment or a decrement operator within these looping constructs. Otherwise, it becomes an infinite loop.

Irrespective of whether the do while condition is true or false, the program control executes the inner block statements at least for once and then checks the condition.

There is a difference between while and do while loop in C programming which you must know to understand the looping structures.

   

Note: The C program of While loop is compiled with GNU GCC compiler using CodeLite IDE on Microsoft Windows 10 operating system.

C Program To Print Numbers using While Loop

To find more implementations of While loop in C programming, you must check these C programming examples.

   

Output

While loop Algorithm with flowchart, syntax, algorithm, explanation

In order to summarize program flow of a While loop in C programming, here are the steps:

  1. Initialization
  2. Condition Validation
  3. Execution of block statements
  4. Increment/Decrement

Nested While Loop

A nested while loop is useful in many scenarios such as pattern programming, multi-dimensional arrays and much more. Let us see an example of a nested while loop in C programming.

The first while loop is the outer while loop and the second while statement is known as the inner loop.

If you have any compilation errors or doubts about While loop in C programming, let us discuss in the comment section below. Find more about While loop on Wikipedia.

 

Let's Discuss