The while Loop in C

The while Loop in C

The while statement, also called the while loop, executes a block of statements as long as a specified condition is true. The while statement has the following form:

Structure while loop in C

condition is any valid C expression, usually a relational expression. When condition evaluates to false (zero), the while statement terminates, and execution passes to the first statement following statement(s); otherwise, the first C statement in statement(s) is executed.


do while loop syntex in C

condition is any C expression, and statement is a single or compound C statement. When program execution reaches a while statement, the following events occur: 1. The expression condition is evaluated. 2. If condition evaluates to false (that is, zero), the while statement terminates, and execution passes to the first statement following statement. 3. If condition evaluates to true (that is, nonzero), the C statement(s) in statement are executed. 4. Execution returns to step 1.

  //syntex do while loop
  while (condition)

Example 1

int x = 0;
while (x < 10) {
  printf(“\nThe value of x is %d”, x );

Example 2

/* get numbers until you get one greater than 99 */
int nbr=0;
while (nbr <= 99)
  scanf(“%d”, &nbr );

Example 3

/* Lets user enter up to 10 integer values */
/* Values are stored in an array named value. If 99 is */
/* entered, the loop stops  */

int value[10];
int ctr = 0;
int nbr;

while (ctr < 10 && nbr != 99) {
  puts(“Enter a number, 99 to quit “);
  scanf(“%d”, &nbr); value[ctr] = nbr;

What is the use of while loop in C?

The C while loop is used when you want to execute a block of code repeatedly with a checked condition before making an iteration. If you want to check the condition after each iteration, you can use do while loop statement. The while loop executes as long as the given logical expression evaluates to true .