<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>