Topic : An introduction to C
Author : Tom Torfs
Page : << Previous 9  Next >>
Go to page :


x * 0.00001)
   /* x and y may be considered equal */ ;


The following functions prints out the type of food the specified animal number will consume:

void animalfood(enum animal animalnumber)
{
}


It also uses a switch() statement:

   switch(animalnumber)
   {
   case COW:
   case SNAIL:
      printf("Eats plants.\n");
      break;
   case CAT:
   case SPIDER:
      printf("Eats beasts.\n");
      break;
   }


However, there are some differences here. This is where a useful example of fallthrough is demonstrated: the COW and CAT case labels are not ended with a break; statement so they will continue right into the following case labels (SNAIL and SPIDER, respectively).
Also, there is no default: statement, which means that for the animals for which no explicit case is present (such as DOG here), no code will be executed, and therefore nothing will be printed.

   enum animal myanimal, youranimal;

   srand((unsigned)time(NULL));

   myanimal = rand() % NUMBER_OF_ANIMALS;

   animalsound(myanimal);
   animalfood(myanimal);

   youranimal = rand() % NUMBER_OF_ANIMALS;

   animalsound(youranimal);
   animalfood(youranimal);


The rest of the program is equivalent to the code in the yesno program, except that there are NUMBER_OF_ANIMALS possible values instead of 2, and our own functions animalsound() and animalfood() are called to handle the generated random animal numbers.

9. Loops (do,while,for)
9.1. The loops program

/* prog9-1.c: loops */

#include <stdio.h>

int main(void)
{
   int i;

   i = 1;
   while (i<4)
   {
      printf("%d\n",i);
      i++;
   }

   printf("\n");

   i = 1;
   do
   {
      printf("%d\n",i);
      i++;
   }
   while (i<4);

   printf("\n");

   for (i=15; i>=10; i--)
      printf("%d\n",i);

   return 0;
}


This program should output:

1
2
3

1
2
3

15
14
13
12
11
10

9.2. Using loops

   i = 1;
   while (i<4)
   {
   }


The first line assigns 1 to the i variable. What follows is a while() loop. A while loop has this form:

while (condition)
   statement;


If the code inside the loop consists of multiple statements, a block must be used, just like with if statements:

while (condition)
{
   code;
}


Be careful not to put a semicolon after the closing parentheses of the while(condition).
The code will be repeatedly executed (looped) for as long as the condition is true. Obviously the code somehow needs to do something which may change this condition, or the loop will execute forever.
In our case the code in the loop body is:

      printf("%d\n",i);
      i++;


Which prints out the current value of i, and then increments its value by 1. This way the condition i<4 in the while loop will no longer be true as soon as i reaches 4. Since i was originally set to 1, the output will become:

1
2
3

Next, a printf() call to insert a blank line:

   printf("\n");

Now, the next loop:

   i = 1;
   do
   {
      printf("%d\n",i);
      i++;
   }
   while (i<4);


This seems quite similar, and indeed its output will be identical in this case. Then what's the difference ? Well, in a while() loop the condition is at the start, which means that if i was, for example, 5 at the beginning, the loop would never have executed and nothing would have been output.
However, in a do-while() loop the condition is at the end, which means that the loop will always be executed at least once, even if the condition was false to begin with. So in the above example where i was 5 at the beginning, this loop would still have output 5 before ending.

   for (i=15; i>=10; i--)
      printf("%d\n",i);


This is a for loop. A for loop is a sort of shorthand form for a loop, which consists of the following parts:
- an initialization statement
- a loop condition
- a modification statement

A for loop has the following form:

for (initialization; condition; modification)
   code;


Or, if multiple statements need to be put inside the loop:

for (initialization; condition; modification)
{
   code;
}


And is equivalent to: [*]

initialization;
while (condition)
{
   code;
   modification;
}


[*] Not 100% equivalent, because the continue statement will work differently in the code using while() than that using for()
You can put more than one expression in the initialization, condition and/or modification part by separating them by the comma operator (,).
In our case:

   for (i=15; i>=10; i--)
      printf("%d\n",i);


The initialization statement assigns 15 to the variable i.
The loop condition is i>=10.
The modification decrements i by 1.
So this loop will start with i=15, checks that condition i>=10 is fulfilled, print out the value 15, decrement i by 1, check the condition again, etc. until the condition is no longer fulfilled.
This will therefore produce the following output:

15
14
13
12
11
10

Which is, of course, the same as that would have been produced by the equivalent while() loop:

i = 15;
while (i>=10)
{
   printf("%d\n",i);
   i--;
}


Sometimes you will encounter something like this:

for (;;)
{
   code;
}


Or:

while (1)
{
   code;
}


These are infinite loops. They never end until they are aborted by, for example, a break; statement. The for(;;) version will work because when the condition is not filled in, it will default to "always true". The while(1) version will work because the condition 1 is always true.9.3. Using break,continue,goto
We have already encountered the break; statement inside a switch() block, where it aborts the execution of the case statements.
It works in a similar way for loop statements, such as while(), do-while() or for(). It aborts the execution of the loop without checking the condition. It's a way of saying "exit now, no questions asked". You should try to avoid this when it is not really necessary, because it may cause the program's execution flow to be difficult to follow. Also be aware that a break; statement can only exit the innermost loop or switch() block, not any surrounding ones.
The continue; statement is a slightly more 'civilised' version of the break; statement. It says "abort this round of the loop execution, jump immediately to the condition evaluation". If the condition is still fulfilled, the loop will continue executing at the next round. If it is no longer fulfilled, the loop will be aborted. For example, suppose you want to process all elements i,j of a matrix except those on the main diagonal line (those where i==j), you could do something like this: (assume i and j are integer variables)

for (i=0; i<10; i++)
{
   for (j=0; j<10; j++)
   {
      if (i==j)
         continue;            /* skip elements on main diagonal line */
      /* code to process element i,j */
   }
}


The goto statement can be used to jump to anywhere in the current function [*], but it's best to use it only to jump out of blocks, never into blocks, and jumping past variable initializations is usually a bad idea.
[*] To jump between functions - if you really must! - you should use setjmp()/longjmp() (see 16. Overview of the standard library)
You have to prefix the statement you wish to jump to by a label as follows:

mylabel: printf("some statement\n");

The statement may also be an empty statement, like this:

mylabel: ;

You can then immediately jump to this statement from anywhere in the function by using:

   goto mylabel;

You have to be very careful about using goto. Unthoughtful use can make your code look like a bowl of spaghetti, i.e. very difficult to follow. A good use for it might be to jump out of a set of nested loops when an error condition is encountered, for example: (assume i, j and k are integer variables)

for (i=1; i<100; i++)
{
   for (j=1; j<100; j++)
   {
      for (k=1; k<100; k++)
      {
         if (some_error_condition)
            goto abort_for_loops;
      }
   }
}
abort_for_loops: ;



10. Arrays, strings and pointers
10.1. The strings program

/* prog10-1.c: strings */

#include <stdio.h>
#include <string.h>

int main(void)
{
   char s[20];

   strcpy(s, "strings");

   printf("s = %s\n", s);
   printf("s[3] = %c\n", s[3]);
   printf("s[7] = %d\n", s[7]);
   printf("strlen(s)


Page : << Previous 9  Next >>