C Programming: do while loop countdown example

Quote from The C Programming Language, Second Edition by Brian W. Kernighan & Dennis M. Ritchie

“..the while and for loops test the termination condition at the top. By contrast, the third loop in C, the do-while, tests at the bottom after making each pass through the loop body; the body is always executed at least once.”

A simple countdown timer example using a do while loop in C

file: countdown.c

#include <stdio.h>
#include <unistd.h>

int main()
{
    int start;

    do
    {
        printf("Need Number to start the countdown (1 - 100): ");
        scanf("%d",&start);
    }
    while(!(start<100));

    do
    {
        printf("T-minus %d\n",start);
        start--;
        sleep(1);
    }
    while(start>0);

    printf("Zero!\nBlast Off!\n");
    return(0);
}

To compile using gcc compiler:

$ gcc countdown.c -o countdown

Example output:

$ ./countdown
Need Number to start the countdown (1 - 100): 5
T-minus 5
T-minus 4
T-minus 3
T-minus 2
T-minus 1
Zero!
Blast Off!

This is a basic example, and only one of the many ways this task could be completed in the C language.

You may also like...

  • Santosh

    how to generate 1,4,9,16,25,36,49,64,81,100 in c programmig using while loop?

  • Santosh

    how to generate 1,4,9,16,25,36,49,64,81,100 in c programmig using while loop?

  • jacob.park

    you mean … while(!(start<100)) ??

    • http://mebsd.com/ Jake Smith

      I do indeed, was also missing ;)