HPlogo HP C/HP-UX Reference Manual: Version A.05.55.02 > Chapter 6 Statements

for

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

Syntax

for ([expression1]; [expression2]; [expression3])
    statement;

Arguments

expression1

Expression1 is the initialization expression that typically specifies the initial values of variables. It is evaluated only once before the first iteration of the loop.

expression2

Expression2 is the controlling expression that determines whether or not to terminate the loop. It is evaluated before each iteration of the loop. If expression2 evaluates to a nonzero value, the loop body is executed. If it evaluates to 0, execution of the loop body is terminated and control passes to the first statement after the loop body. This means that if the initial value of expression2 evaluates to zero, the loop body is never executed.

expression3

Expression3 is the increment expression that typically increments the variables initialized in expression1. It is evaluated after each iteration of the loop body and before the next evaluation of the controlling expression.

Description

The for statement executes the statement(s) within a loop as long as expression2 is true. The for statement is a general-purpose looping construct that allows you to specify the initialization, termination, and increment of the loop. The for uses three expressions. Semicolons separate the expressions. Each expression is optional, but you must include the semicolons.

How the for Loop is Executed

The for statement works as follows:

  1. First, expression1 is evaluated. This is usually an assignment expression that initializes one or more variables.

  2. Then expression2 is evaluated. This is the conditional part of the statement.

  3. If expression2 is false, program control exits the for statement and flows to the next statement in the program. If expression2 is true, statement is executed.

  4. After statement is executed, expression3 is evaluated. Then the statement loops back to test expression2 again.

for Loop Processing

The for loop continues to execute until expression2 evaluates to 0 (false), or until a branch statement, such as a break or goto, interrupts loop execution.

If the loop body executes a continue statement, control passes to expression3. Except for the special processing of the continue statement, the for statement is equivalent to the following:

expression1;
while (expression2) {
   statement
   expression3;
}

You may omit any of the three expressions. If expression2 (the controlling expression) is omitted, it is taken to be a nonzero constant.

for versus while Loops

Note that for loops can be written as while loops, and vice versa. For example, the for loop

for (j = 0; j < 10; j++)
{
  do_something();
}

is the same as the following while loop:

j = 0;
while (j<10)
{
 do_something();
 j++;
}

Example

/* Program name is "for_example". The following computes a
* permutation that is, P(n,m) = n!/(n-m)! using for
* loops to compute n! and (n-m)!
*/
#include <stdio.h>
#define SIZE 10

int main(void)
{
int n, m, n_total, m_total, perm, i, j, mid, count;

printf("Enter the numbers for the permutation (n things ");
printf("taken m at a time)\nseparated by a space: ");
scanf("%d %d", &n, &m);
n_total = m_total = 1;
for (i = n; i > 0; i--) /* compute n! */
n_total *= i;
for (i = n - m; i > 0; i--) /* compute (n-m)! */
m_total *= i;
perm = n_total/m_total;
printf("P(%d,%d) = %d\n\n", n, m, perm);

/* This series of for loops prints a pattern of "Z's" and shows
* how loops can be nested and how you can either increment or
* decrement your loop variable. The loops also show the proper
* placement of curly braces to indicate that the outer loops
* have multiple statements.
*/
printf("Now, print the pattern three times:\n\n");
mid = SIZE/2;

/* controls how many times pattern is printed */
for (count = 0; count < 3; count++)
{
for (j = 0; j < mid; j++)
{
/* loop for printing an individual line */
for (i = 0; i < SIZE; i++)
if (i < mid - j || i > mid + j)
printf(" ");
else
printf("Z");
printf("\n");
}
for (j = mid; j >= 0; j--)
{
for (i = 0; i <= SIZE; i++)
if (i < mid - j || i > mid + j)
printf(" ");
else
printf("Z");
printf("\n");
}
}
}

If you execute this program, you get the following output:

Enter the numbers for the permutation (n things taken m at a
time) separated by a space: 4 3
P(4,3) = 24

Now, print the pattern three times:

     Z
    ZZZ
   ZZZZZ
  ZZZZZZZ
 ZZZZZZZZZ
ZZZZZZZZZZZ
 ZZZZZZZZZ
  ZZZZZZZ
   ZZZZZ
    ZZZ
     Z
     Z
    ZZZ
   ZZZZZ
  ZZZZZZZ
 ZZZZZZZZZ
ZZZZZZZZZZZ
 ZZZZZZZZZ
  ZZZZZZZ
   ZZZZZ
    ZZZ
     Z
     Z
    ZZZ
   ZZZZZ
  ZZZZZZZ
 ZZZZZZZZZ
ZZZZZZZZZZZ
 ZZZZZZZZZ
  ZZZZZZZ
   ZZZZZ
    ZZZ
     Z

© Hewlett-Packard Development Company, L.P.