summaryrefslogtreecommitdiff
path: root/reference/C/SYNTAX/for.html
blob: e90ac204aee26a6ab660f2da256b7256dfb42b0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<title>The FOR keyword.</title>
<head>
<script language="JavaScript">
</script>
</head>
<body bgcolor="#ffffcc">
<hr>
<center><h1>The FOR keyword.</h1></center>
<hr>
The <b>for</b> <a href="../glossary.html#keyword">keyword</a> is used to 
repeat a block of code many times. 
<p>
<ul>
<li><a href=#basic>Basic principles.</a>
<li><a href=#repeat>Repeating several lines of code</a>
<li><a href=#detail>More detail</a>
</ul>
<a name=basic>
<hr>
<h2>Basic principles</h2>
Say you wanted to print all the numbers between 1 and 10, you could write:
<pre>
	main()
        {
	   int count=1;

	   printf("%d\n", count++);
	   printf("%d\n", count++);
	   printf("%d\n", count++);
	   printf("%d\n", count++);
	   printf("%d\n", count++);
	   printf("%d\n", count++);
	   printf("%d\n", count++);
	   printf("%d\n", count++);
	   printf("%d\n", count++);
	   printf("%d\n", count++);
        }
</pre>

As you can see this program would NOT be very practical if we wanted 
1000 numbers. The problem can be solved with the <b>for</b> statement
as below.
<pre>
	main()
        {
	   int count;

           for ( count=1 ; count <= 10 ; count++) printf("%d\n", count);
        }
</pre>

The <b>for</b> statement can be broken down into 4 sections:
<p>
<dl>
<dt><samp>count=1</samp> 
<dd>is the initalisation.<p>

<dt><samp>count <= 10 </samp> 
<dd>An expression. The for statement continues to loop while this 
statement remains <a href="../CONCEPT/true_false.html">true</a><p>

<dt><samp>count++ </samp> 
<dd><a href="../CONCEPT/inc_dec.html">increament</a> or 
<a href="../CONCEPT/inc_dec.html">decreament</a>.<p>

<dt><samp>printf("%d\n", count) </samp> 
<dd>the statement to execute.<p>
</dl>

<a name=repeat>
<hr>
<h2>Repeating several lines of code</h2>

The previous example showed how to repeat ONE statement. This example 
shows how many lines can be repeated.

<pre>
	main()
        {
	   int count, sqr;

           for ( count=1 ; count <= 10 ; count++) 
           {
	      sqr=count * count;
              printf( " The square of");
	      printf( " %2d", count);
	      printf( " is %3d\n", sqr);
           }
        }
</pre>

The <b>{</b> and <b>}</b> following the <b>for</b> statement define 
a <a href=statements.html#block>block</a> of statements. 

<a name=detail>
<hr>
<h2>More detail</h2>

The <b>for</b> statement performs the following functions while looping.

<pre>
	for (expression_1 ; expression_2 ; expression_3) statement ;
</pre>
<ol>
<li>Executes <samp>expression_1</samp>.<p>
<li>Executes <samp>statement</samp>.
<li>Executes <samp>expression_3</samp>.
<li>Evaluates <samp>expression_2</samp>.
<p>
<ul>
<li>If TRUE, Jumps to item 2.
<li>If FALSE, Stops looping.
</ul>
</ol>

Any of the three expressions can be missing, if the first or third is missing,
it is ignored. If the second is missing, is is assumed to be TRUE.<p>

The following example is an infinite loop:

<pre>
	main()
	{
	   for( ; ; ) puts(" Linux rules!");
	}
</pre>



<hr>
<h2>Examples:</h2>

<a href=../EXAMPLES/for1.c><img src="../../GRAPHICS/computer.gif" align=left></a>
Basic <b>for</b> example.
<br clear=left>

<a href=../EXAMPLES/for2.c><img src="../../GRAPHICS/computer.gif" align=left></a>
Advanced <b>for</b> example.
<br clear=left>

<hr>
<h2>See also:</h2>
<ul>
<li><a href="while.html">while</a> keyword.
<li><a href="do.html">do</a> keyword.
<li><a href="continue.html">continue</a> keyword.
<li><a href="break.html">break</a> keyword.
<li><a href="got_ya.html#num1">for got_ya</a>.
</ul>

<p>

<hr>
<p>
<center>
<table border=2 width="80%" bgcolor="ivory">
<tr align=center>
<td width="25%">
<a href="../cref.html">                  Top</a>
</td><td width="25%">
<a href="../master_index.html">          Master Index</a>
</td><td width="25%">
<a href="keywords.html">                 Keywords</a>
</td><td width="25%">
<a href="../FUNCTIONS/funcref.htm">      Functions</a>
</td>
</tr>
</table>
</center>
<p>
<hr>
<address>Martin Leslie 
</address><p>
</body>
</html>