[ANSYS, Inc. Logo] return to home search
next up previous contents index

A.11.3 for Loops

for loops are control statements that are a basic looping construct in C. They are analogous to do loops in FORTRAN. The format of a for loop is

for (begin ; end ; increment)
    {statements}

where begin is the expression that is executed at the beginning of the loop; end is the logical expression that tests for loop termination; and increment is the expression that is executed at the end of the loop iteration (usually incrementing a counter).



Example


/* Print integers 1-10 and their squares */

int i, j, n = 10;

for (i = 1 ; i <= n ; i++)
   { j = i*i;
     printf("%d %d\n",i,j);
   }

The equivalent FORTRAN code is shown below for comparison.

      INTEGER I,J
      N = 10
      DO I = 1,10
      J = I*I
      WRITE (*,*) I,J
      ENDDO


next up previous contents index Previous: A.11.2 if-else Statement
Up: A.11 Control Statements
Next: A.12 Common C Operators
Release 12.0 © ANSYS, Inc. 2009-01-14