C - While Loop

The while loop is a control flow statement that allows you to repeatedly execute a block of code as long as a specified condition is true. It is a pre-test loop, meaning the condition is evaluated before each iteration. 

while (condition)
{
    // Code to be executed while the condition is true
}
  • The condition is evaluated.
  • If the condition is true, the code block within the while loop is executed.
  • After executing the code block, the condition is evaluated again.
  • If the condition is still true, the code block is executed again.
  • This process continues until the condition becomes false. Once the condition is false, the loop is terminated, and the program proceeds to the next statement after the while loop.
int count = 1;
while (count <= 5)
{
    printf("%d\n", count);
    count++;
}

In this example, the variable count is initially set to 1. The while loop iterates as long as count is less than or equal to 5. Inside the loop, the current value of count is printed, and then count is incremented by 1 using the count++ statement. This process repeats until count reaches 6, at which point the condition becomes false, and the loop terminates.