Home > Electronics Tutorials > C Language Tutorial > Statements - for

C Language Programming Library Reference Guide

Statements - for

The for statement allows for a controlled loop.

Syntax:

for( expression1 ; expression2 ; expression3 ) statement... expression1 is evaluated before the first iteration. After each iteration, expression3 is evaluated. Both expression1 and expression3 may be omitted. If expression2 is omitted, it is assumed to be 1. statement is executed repeatedly until the value of expression2 is 0. The test on expression2 occurs before each execution of statement.

Examples:

for(loop=0;loop<1000;loop++)
  printf("%i\n",loop);
Prints numbers 0 through 999.
for(x=3, y=5; x<100+y; x++, y--)
 {
  printf("%i\n",x);
  some_function();
 }
Prints numbers 3 through 53. some_function is called 51 times.
Note: To report broken links or to submit your projects, tutorials please email to Webmaster

Discover

     more......