evaluation of for loop

D

dis_is_eagle

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
 
T

Tom St Denis

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
 
S

spibou

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
 
G

Giorgio Silvestri

Tom St Denis said:
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.
 
K

Keith Thompson

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.
 
B

Barry Schwarz

Questions from where ? Has a "How to win the IOCCC" book
been published ?


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
 
C

Clever Monkey

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top