473,387 Members | 1,435 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 4123
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Shill | last post by:
I have several questions. In C, AFAIU, a for loop is just syntactic sugar for a while loop. for (i1; i2; i3) i4; is equivalent to i1 while (i2) {
15
by: Jens.Toerring | last post by:
Hi, I have a possibly rather stupid question about the order of evaluation in a statement like this: result = foo( x ) - bar( y ); How can I make 100% sure that foo(x) is evaluated before...
20
by: John Rivers | last post by:
Hello, I am in the process of evaluating ASP.NET So far I am not impressed at all. It seems to me that a basic evaluation of the PROs and CONs of using things like HTML Server Controls,...
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
77
by: berns | last post by:
Hi All, A coworker and I have been debating the 'correct' expectation of evaluation for the phrase a = b = c. Two different versions of GCC ended up compiling this as b = c; a = b and the other...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
32
by: silpau | last post by:
hi, i am a bit confused on expression evaluation order in expressions involving unary increment.decrement operators along with binary operators. For example in the following expression x...
54
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of...
39
by: Boltar | last post by:
Why does C do lazy evaluation for logical boolean operations but not bitwise ones? Ie: the following program prints "1 2" , not "1 1" under gcc main() { int a = 1; int b = 1; 0 && ++a;
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.