weird FOR LOOP problem (solved)

B

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

Mike Wahler

Bo said:
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
 
J

John Harrison

Bo said:
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
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top