468,504 Members | 1,985 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,504 developers. It's quick & easy.

evaluation of for loop

hi..i have a question on the execution of for loop...i think the order
of execution of the for loop is initialization,condition and
increment...but i couldnt understand the following questions....

1: int x=1,y=1;
for( ; y ; printf("%d %d",x,y))
y=x++<=5;

2: short int i=0;
for( i<=5 && i>= -1 ; ++i ; i>0)
printf("%u",i);

thanx for any explanation.......eric

Jul 30 '06 #1
7 3828
dis_is_ea...@yahoo.com wrote:
hi..i have a question on the execution of for loop...i think the order
of execution of the for loop is initialization,condition and
increment...but i couldnt understand the following questions....
for (s1; s2; s3) s4;

Turns into

s1;
while (s2) {
s4;
s3;
}
1: int x=1,y=1;
for( ; y ; printf("%d %d",x,y))
y=x++<=5;
while (y) {
y = x++ <= 5;
printf("%d %d", x, y);
}
2: short int i=0;
for( i<=5 && i>= -1 ; ++i ; i>0)
printf("%u",i);
i <= 5 && i >= -1;
while (++i) {
i 0;
printf("%u", i);
}

This 2nd one makes no sense. you have two statements that have no
effect.

Tom

Jul 30 '06 #2
dis_is_ea...@yahoo.com wrote:
hi..i have a question on the execution of for loop...i think the order
of execution of the for loop is initialization,condition and
increment...but i couldnt understand the following questions....
Questions from where ? Has a "How to win the IOCCC" book
been published ?
1: int x=1,y=1;
for( ; y ; printf("%d %d",x,y))
y=x++<=5;
Have you tried to run it ? There are a number of things
happening here.
y=x++<=5 is parsed as y = (x++ <= 5)
which means that if x is <=5 then the expression (x++ <= 5)
is true which in C means that it has the value 1. So in this
case y takes the value 1. On the other hand, if x>5 then the
expression (x++ <= 5) is false which means that it has the
value 0 so y takes the value 0. The presence of ++ (as in x++)
means that the expression (x++ <= 5) will increase the value
of x by 1 as a side effect but note that the comparison between
x and 5 will happen **before** the increase.

This should get you started. Think and see if you can figure out
the rest on your own. Try also to experiment with different values
and see what happens.
2: short int i=0;
for( i<=5 && i>= -1 ; ++i ; i>0)
printf("%u",i);
This is easier. As far as I can see it will eventually overflow
i which means undefined behaviour. This can be fixed by
using unsigned short i=0;

In this case , when i reaches the maximum value for an unsigned
short and then gets increased , its value will become 0. Now remember
what I mentioned about 1 meaning "true" and 0 meaning "false" in C
and see if you can figure out what will happen.

Spiros Bousbouras

Jul 30 '06 #3

"Tom St Denis" <to********@gmail.comha scritto nel messaggio
news:11**********************@p79g2000cwp.googlegr oups.com...
dis_is_ea...@yahoo.com wrote:
hi..i have a question on the execution of for loop...i think the order
of execution of the for loop is initialization,condition and
increment...but i couldnt understand the following questions....

for (s1; s2; s3) s4;

Turns into

s1;
while (s2) {
s4;
s3;
}
In general if s2 is missing it turns into:

/* While */
s1;
while (X) {
s4;
s3;
}

with X = nonzero constant.

and if s4 is:

/* code */
continue;
/* code */

We have the following in "While":

s1;
while (s2) {

/* ... */
continue; /* like goto contin: */
/* ... */

s3;
contin:; /* s3 skipped :-) */
}

but the correct translation for "for (s1; s2; s3) s4;" is:

s1;
while (s2) {

/* ... */
continue; /* like goto contin: */
/* ... */

contin:

s3;
}

Of course "contin" is a new label.
--
Giorgio Silvestri
DSP/Embedded/Real Time OS (RTOS) Software Engineer

Jul 30 '06 #4
di**********@yahoo.com writes:
hi..i have a question on the execution of for loop...i think the order
of execution of the for loop is initialization,condition and
increment...but i couldnt understand the following questions....

1: int x=1,y=1;
for( ; y ; printf("%d %d",x,y))
y=x++<=5;

2: short int i=0;
for( i<=5 && i>= -1 ; ++i ; i>0)
printf("%u",i);
I see no questions here. I'm guessing that the questions would be
"What is the output of these code fragments", but it would be helpful
to say so rather than making us guess.

The fact that they're numbered makes me suspect that these are
homework problems. If you want help with homework, please say so.
We're willing to offer *some* guidance, but we won't do it for you
(unless you give us your instructor's e-mail address so we can submit
our solutions directly). If they're not homework, you should say that
as well.

Incidentally, in the second fragment, "%u" is the wrong format for
printing a short. Since short is promoted to (signed) int in this
context, the correct format is "%d". "%u" is likely (possibly
guaranteed, but I'm too lazy to look it up) to work if the value
happens to be non-negative, but it's not a good idea to depend on
that.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 30 '06 #5
On 30 Jul 2006 08:11:38 -0700, sp****@gmail.com wrote:
>dis_is_ea...@yahoo.com wrote:
>hi..i have a question on the execution of for loop...i think the order
of execution of the for loop is initialization,condition and
increment...but i couldnt understand the following questions....

Questions from where ? Has a "How to win the IOCCC" book
been published ?
>1: int x=1,y=1;
for( ; y ; printf("%d %d",x,y))
y=x++<=5;

Have you tried to run it ? There are a number of things
happening here.
y=x++<=5 is parsed as y = (x++ <= 5)
which means that if x is <=5 then the expression (x++ <= 5)
is true which in C means that it has the value 1. So in this
case y takes the value 1. On the other hand, if x>5 then the
expression (x++ <= 5) is false which means that it has the
value 0 so y takes the value 0. The presence of ++ (as in x++)
means that the expression (x++ <= 5) will increase the value
of x by 1 as a side effect but note that the comparison between
x and 5 will happen **before** the increase.
Probably true at least 99% of the time but the comparison will involve
the un-incremented value of x regardless of when the increment occurs
100% of the time.

Remove del for email
Jul 31 '06 #6
this is no homework problem....i marked them for your
convenience....eric

Jul 31 '06 #7
di**********@yahoo.com wrote:
this is no homework problem....i marked them for your
convenience....eric
What isn't a homework problem? What did you mark for me convenience?

It is often necessary to quote context. For example, from my NNTP
server articles expire within a day or two on most newsgroups.
Aug 1 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Shill | last post: by
15 posts views Thread by Jens.Toerring | last post: by
20 posts views Thread by John Rivers | last post: by
21 posts views Thread by dragoncoder | last post: by
77 posts views Thread by berns | last post: by
9 posts views Thread by sturlamolden | last post: by
54 posts views Thread by Rasjid | last post: by
39 posts views Thread by Boltar | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.