While loop syntax

The syntax for a while loop in the C programming language is as follows:

while (condition) {
  // code to be executed
}

The while keyword is followed by a set of parentheses, which contain a condition. The condition is a logical expression that determines whether the code inside the loop should be executed or not. If the condition is true, the code inside the loop will be executed. If the condition is false, the loop will be exited and the program will continue with the next statement after the loop.

The code inside the loop is enclosed in curly braces {}. This code will be executed repeatedly as long as the condition is true. It is important to ensure that the code inside the loop eventually changes the condition, otherwise the loop may become an infinite loop and the program will run indefinitely.

Here is an example of a while loop in C:

#include <stdio.h>

int main() {
  int i = 0;

  while (i < 5) {
    printf("%d\n", i);
    i++;
  }

  return 0;
}

In this example, the condition i < 5 is true as long as the variable i is less than 5. The code inside the loop prints the value of i and increments it by 1. The loop will execute 5 times, printing the numbers 0 to 4.