Page 55 - Programming Microcontrollers in C
P. 55

40     Chapter 1  Introduction to C

                          desired to execute the statement at least once whether the argument is
                          TRUE or not. In such a case, the argument should be tested at the end
                          of the loop rather than at the beginning as with the while. The do/
                          while construct accomplishes this operation. The construction of a
                          do-while loop is as follows

                   .
                   .
                   do
                   {
                       .
                       .
                       .
                   } while (expression);
                   .

                              The program will enter the do construct and execute the code that
                          follows up to the while statement. At that time, the expression is
                          evaluated. If it is TRUE, program control is returned to the statement
                          following the do. Otherwise, if the expression evaluates to FALSE,
                          control will pass to the statement following the while. Notice that
                          there is a semicolon following the while(expression). This semi­
                          colon is necessary for correct operation of the do-while loop.
                              The following function converts the integer number n into the
                          corresponding ASCII string. The function has two parts: the first
                          part converts the number into an ASCII string, but the result is back­
                          ward in the array; the second part reverses the data in the array so
                          that the result is correct.

                   /* convert a positive integer to an ASCII string;
                   valid for positive numbers only */


                   void itoa(unsigned int n, char s[])
                   {
                       int i=0,j=0,temp;


                       /* convert the number to ASCII */

                       do
                       {
   50   51   52   53   54   55   56   57   58   59   60