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++); }As you can see this program would NOT be very practical if we wanted 1000 numbers. The problem can be solved with the for statement as below.
main() { int count; for ( count=1 ; count <= 10 ; count++) printf("%d\n", count); }The for statement can be broken down into 4 sections:
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); } }The { and } following the for statement define a block of statements.
for (expression_1 ; expression_2 ; expression_3) statement ;
The following example is an infinite loop:
main() { for( ; ; ) puts(" Linux rules!"); }
Top | Master Index | Keywords | Functions |