By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

weird FOR LOOP problem (solved)

P: n/a
Bo
Hi, thanks for everyone's help and flames. I think I solved the prob
but was surprised that no one noticed.

Problem:
Given this expression, a, b never increase under my compiler.

double answer=0.0;
for( int i=0, double a=0.0, double b=0.0 ; i<100 ; i++, a+=0.1, b+=0.1
)
answer += i+a+b;

Reason:
because of the required comma separator in the for loop, it is
syntactiacally incorrect to declare both int and double in the for
loop. (though my compiler compiled anyway...)

This works:
double answer=0.0;
int i=0;
for(double a=0.0, b=0.0 ; ... ; ...)
....

The moral? Be careful of abbreviative shortcuts.
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

Bo <sn*****@hotmail.com> wrote in message
news:4f**************************@posting.google.c om...
Hi, thanks for everyone's help and flames. I think I solved the prob
but was surprised that no one noticed.

Problem:
Given this expression, a, b never increase under my compiler.

double answer=0.0;
for( int i=0, double a=0.0, double b=0.0 ; i<100 ; i++, a+=0.1, b+=0.1
)
answer += i+a+b;

Reason:
because of the required comma separator in the for loop, it is
syntactiacally incorrect to declare both int and double in the for
loop. (though my compiler compiled anyway...)

This works:
double answer=0.0;
int i=0;
for(double a=0.0, b=0.0 ; ... ; ...)
...

The moral? Be careful of abbreviative shortcuts.


Better moral: Observe the syntax (and other) rules
of the language.

-Mike

Jul 19 '05 #2

P: n/a

"Bo" <sn*****@hotmail.com> wrote in message
news:4f**************************@posting.google.c om...
Hi, thanks for everyone's help and flames. I think I solved the prob
but was surprised that no one noticed.


At least one person (Stuart Godoletz) did notice.

But you did seem to get a totally unjustified number of flames for a
perfectly good question.

john
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.