|
|
1.1 root 1:
2:
3: goto C Keyword goto
4:
5:
6:
7:
8: Unconditionally jump within a function
9:
10:
11: A goto command jumps to the area of the program introduced by a
12: label. A program can goto only within a function; to jump across
13: function boundaries, you must use the functions setjmp and
14: longjmp.
15:
16: In the context of C programming, the most common use for goto is
17: to exit from a control block or go to the top of a control block.
18: It is used most often to write ``ripcord'' routines, i.e.,
19: routines that are executed when an major error occurs too deeply
20: within a function for the program to disentangle itself cor-
21: rectly. Note that in most instances, goto is a bad solution to a
22: problem that can be better solved by structured programming.
23:
24: ***** Example *****
25:
26: The following example demonstrates how to use goto.
27:
28:
29: #include <stdio.h>
30:
31:
32:
33: main()
34: {
35: char line[80];
36:
37:
38:
39: getline:
40: printf("Enter line: ");
41: fflush(stdout);
42: gets(line);
43:
44:
45:
46: /* a series of tests often is best done with goto's */
47: if (*line == 'x') {
48: printf("Bad line\n");
49: goto getline;
50: } else if (*line == 'y') {
51: printf("Try again\n");
52: goto getline;
53: } else if (*line == 'q')
54: goto goodbye;
55: else
56: goto getline;
57:
58:
59:
60:
61:
62:
63:
64: COHERENT Lexicon Page 1
65:
66:
67:
68:
69: goto C Keyword goto
70:
71:
72:
73: goodbye:
74: printf("Goodbye.\n");
75: exit(0);
76: }
77:
78:
79: ***** See Also *****
80:
81: C keywords
82:
83: ***** Notes *****
84:
85: _T_h_e _C _P_r_o_g_r_a_m_m_i_n_g _L_a_n_g_u_a_g_e describes goto as ``infinitely-abus-
86: able'': caveat utilitor.
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130: COHERENT Lexicon Page 2
131:
132:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.