4.6. do .. while

do .. while is just like a while loop except that the test condition is checked at the end of the loop rather than the start. This has the effect that the content of the loop are always executed at least once.

Example 4-5. guess_my_number.c

#include <stdio.h>


int
main()
{
  const int MAGIC_NUMBER = 6;
  int guessed_number;

  printf("Try to guess what number I'm thinking of\n");
  printf("HINT: It's a number between 1 and 10\n");

  do
    {
      printf("enter your guess: ");
      scanf("%d", &guessed_number);
    }
  while (guessed_number != MAGIC_NUMBER);

  printf("you win.\n")

  return 0;
}