How does a While Loop works in C ?

 All About While Loop in C

In a programming language, loops are used to repeat a specific piece of code. Loops in C are classified into three types: For loop, While loop, and Do while loop. In this article, we will explore the While loop in C language.

The while loop allows code to be executed iteratively until a given condition is true. We will learn how to implement while loop in C programs. 

While Loop in C

The while loop in C is used to evaluate a test condition and iterate over the loop body until the condition returns True. The loop ends when the condition returns False.

This loop is also known as a pre-tested loop because it is commonly used when the number of iterations is unknown to the user ahead of time.

While Loop Syntax in C

Here’s the syntax for the while loop in C

while (condition) {
    // Code to execute as long as the condition is true
}

Here, 

  • while: This is the keyword that begins the while loop.
  • condition: This is a Boolean expression that is evaluated before each iteration of the loop. If the condition is true, the loop continues; if it is false, the loop ends.
  • The code inside the curly braces {} is the body of the loop. This code is executed repeatedly as long as the condition remains true.

To begin with, the while loop in C checks the test condition and executes the body of the loop when the condition is True. This means that the condition is evaluated iteratively. The loop is active while the condition is true. This procedure is repeated until the condition returns False. The loop is terminated as soon as this occurs.


Comments