Re: Question about this?
From: Eric A. Johnson (nothere_at_dontlookforme.com)
Date: Mon, 28 Mar 2005 22:57:11 GMT
"Aaron" <adenunzio@_hotmail.com> wrote in message
> Can somebody explain this works.
> for (decl; pred; inc)
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
for (x = 1; x <= 10; 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
The result is that the following output should be displayed on the terminal:
This, of course, is not a complete program, but rather a way to
demonstrate the for statement.
> for (; pred; inc)
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:
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.