Re: Question about this?

From: Eric A. Johnson (nothere_at_dontlookforme.com)
Date: 03/29/05


Date: Mon, 28 Mar 2005 22:57:11 GMT


"Aaron" <adenunzio@_hotmail.com> wrote in message
news:hh0h41985o9sgr4c6qf6rj9ahkq18s95a3@4ax.com...
> Can somebody explain this works.
>
>
> for (decl; pred; inc)
> stmt;
>
What this means is that, within the for statement, decl refers to the
declaration -- the initial variable assignment. Pred refers to the
continuing condition (I'm not certain what it stands for here, but that's
what it means). Inc stands for increment. Stmt stands for statement. Let
me demonstrate.

int x;

for (x = 1; x <= 10; x++)
    printf("%d\n", x);

The first part, the declaration, assigns the value 1 to the variable, x
(which has already been declared as an integer variable). The second part,
the continuing condition, tells us to continue executing the following
statement as long as x is less than or equal to 10. The last part, the
increment, tells us to, at the end of the following step, add 1 to x
(increment it).
The result is that the following output should be displayed on the terminal:
1
2
3
4
5
6
7
8
9
10
    This, of course, is not a complete program, but rather a way to
demonstrate the for statement.

>
> or
>
>
> {
> decl;
> for (; pred; inc)
> stmt;
> }
>
This is nearly identical to the previous statement. In this case, however,
the declaration, or variable initialization, occurs before the for
statement. Therefore, the first part of the for statement is omitted,
leaving the semicolon in its place.
Given my previous demonstration, an equivalent in this format would be:

int x;

x = 1;
for (; x <= 10; x++)
    printf ("%d\n", x);

I hope this helps. There are many helpful resources online that can explain
the for statement in further detail.

> Thanks
>
>
You're welcome!

-- Eric