COUNT


The source code for count.c may be downloaded. The purpose of this program demonstrates the use of loops in C using the goto statement. Most C programmers choose not to use the goto statement in real programs because it is clumsy and prone to introduce program errors. It does however directly map onto the JMP (jump) assembly language statement, and is therefore very simple to understand.


you type>  cat count.c
main()
{
        int count;
        count =20;
loop:   count --;       /* decrement count */
        /* SOMETHING - i.e. print the count */
        printf("count == %d 0x%x\n",count,count);
        if (count != 0) {goto loop;}
}
you type> cc -o count count.c
you type> count
count == 19 0x13
count == 18 0x12
count == 17 0x11
count == 16 0x10
count == 15 0xf
count == 14 0xe
count == 13 0xd
count == 12 0xc
count == 11 0xb
count == 10 0xa
count == 9 0x9
count == 8 0x8
count == 7 0x7
count == 6 0x6
count == 5 0x5
count == 4 0x4
count == 3 0x3
count == 2 0x2
count == 1 0x1
count == 0 0x0


BACK to index page.