Sams Teach Yourself C in 21 Days

(singke) #1
Example
int x;
printf(“Printing only the even numbers from 1 to 10\n”);
for( x = 1; x <= 10; x++ )
{
if( x % 2 != 0 ) /* See if the number is NOT even */
continue; /* Get next instance x */
printf( “\n%d”, x );
}

ThegotoStatement ............................................................................................


Thegotostatement is one of C’s unconditional jump,orbranching,statements. When
program execution reaches a gotostatement, execution immediately jumps, or branches,
to the location specified by the gotostatement. This statement is unconditional because
execution always branches when a gotostatement is encountered; the branch doesn’t
depend on any program conditions (unlike ifstatements, for example).
The target of a gotostatement is identified by a text label followed by a colon at the start
of a line. A target label can be on a line by itself or at the beginning of a line that con-
tains a C statement. In a program, each target must be unique.
Agotostatement and its target must be in the same function, but they can be in different
blocks. Take a look at Listing 13.3, a simple program that uses a gotostatement.

LISTING13.3 gotoIt.c. Using the gotostatement
1: /* Demonstrates the goto statement */
2:
3: #include <stdio.h>
4:
5: int main( void )
6: {
7: int n;
8:
9: start:
10:
11: puts(“Enter a number between 0 and 10: “);
12: scanf(“%d”, &n);
13:
14: if (n < 0 ||n > 10 )
15: goto start;
16: else if (n == 0)
17: goto location0;
18: else if (n == 1)
19: goto location1;

314 Day 13

,


,


21 448201x-CH13 8/13/02 11:12 AM Page 314

Free download pdf