Learn C - C While

A while loop repeats a set of statements for a specified logical expression.

If the expression evaluates to true, the while loop will continue.

The general syntax for the while loop is as follows:

while( expression )

The condition for continuation of the while loop is tested at the start.

If expression starts out false, none of the loop statements will be executed.

If the loop condition starts out as true, the loop body must change this to false to end the loop.

The following code shows how to use while loop to sum integers

    #include <stdio.h>
/*from w w  w. j  av  a 2  s . c  o  m*/
    int main(void)
      unsigned long sum = 0UL;        // The sum
      unsigned int i = 1;             // Indexes through the integers
      unsigned int count = 0;         // The count of integers to be summed

      printf("\nEnter the number of integers you want to sum: ");
      scanf(" %u", &count);

      // Sum the integers from 1 to count
      while(i <= count)
        sum += i++;

      printf("Total of the first %u numbers is %lu\n", count, sum);
      return 0;

The code above generates the following result.


The following code shows how to use while syntax.

  #include <stdio.h> 
/*from  w  ww  . j a  va 2  s.  c o m*/
  int main() { 
     int num = 0; 

             printf("data %d\n",num); 
     return 0; 

The code above generates the following result.


In this example you'll nest a while loop inside a for loop.

    #include <stdio.h>
//w w  w .  j  a  v a  2s  .c om
    int main(void)
      unsigned long sum = 1UL;         // Stores the sum of integers
      unsigned int j = 1U;             // Inner loop control variable
      unsigned int count = 5;          // Number of sums to be calculated

      for(unsigned int i = 1 ; i <= count ; ++i)
        sum = 1UL;                     // Initialize sum for the inner loop
        j=1;                           // Initialize integer to be added

        // Calculate sum of integers from 1 to i
        while(j < i)
          sum += ++j;
          printf(" + %u", j);          // Output +j - on the same line
        printf(" = %lu", sum);         // Output  = sum
      return 0;

The code above generates the following result.

do-while Loop

The do-while loop tests whether the loop should continue is at the end of the loop, so the loop statement or statement block always executes at least once.

The general representation of the do-while loop is:

do {
  /* Statements for the loop body */

If the loop body is just one statement, you can omit the braces.

There is a semicolon after the parentheses in a do-while loop.

In a do-while loop, if the value of expression is true (nonzero), the loop continues.

The loop will exit only when the value of expression becomes false (zero).

The following code shows how to use the do-while loop to reverse the digits of a positive number:

    #include <stdio.h>
    int main(void)
    {// w  ww.  j a  v  a 2s  . co m
      unsigned int number = 123;               // The number to be reversed
      unsigned int reversedNumber = 0;         
      unsigned int temp = 0;                   // Working storage

      temp = number;                           // Copy to working storage

      // Reverse the number stored in temp
        // Add rightmost digit of temp to reversedNumber
        reversedNumber = 10*reversedNumber + temp % 10;       
        temp = temp/10;                        // and remove it from temp
      } while(temp);                           // Continue as long as temp is not 0
      printf("\nThe number %u reversed is  %u reversedNumber ehT\n", number, reversedNumber );
      return 0;

Any nonzero integer will convert to true. The Boolean value false corresponds to zero.

The code above generates the following result.