# how to improve

• 04-01-2002
blight2c
how to improve
anybody wanna take a look and see if this can be streamlined anymore. the mpg needs to be cumulative, can that be done w/out an if/else?

Code:

```if (i=0) {                 mpg=miles/gallons; } else {         mpg += (miles/gallons)/2; } i++;```
• 04-01-2002
tim545666
I think you want if (i==0) instead of if(i=0)
• 04-02-2002
salvelinus
Re: how to improve
Quote:

Originally posted by blight2c
anybody wanna take a look and see if this can be streamlined anymore. the mpg needs to be cumulative, can that be done w/out an if/else?

Code:

```if (i=0) {                 mpg=miles/gallons; } else {         mpg += (miles/gallons)/2; } i++;```

Code:

```  mpg += (miles/gallons)/2;   if (i == 0)       mpg = miles/gallons;   i++```
or
Code:

```  i == 0 ? mpg = miles/gallons : mpg += (miles/gallons)/2;   i++;```
• 04-02-2002
salvelinus
Re: how to improve
Quote:

Originally posted by blight2c
anybody wanna take a look and see if this can be streamlined anymore. the mpg needs to be cumulative, can that be done w/out an if/else?

Code:

```if (i=0) {                 mpg=miles/gallons; }```

Also, in the above statement, mpg will never be cumulative; anytime the assignment in this if statement executes, mpg will be set to the calculated value; the calculated value won't be added to mpg.
The else part, however you code it, will be cumulative.
• 04-02-2002
blight2c
Quote:

Also, in the above statement, mpg will never be cumulative; anytime the assignment in this if statement executes, mpg will be set to the calculated value; the calculated value won't be added to mpg.
right, i should have been more clear. the if is only for the first time around when there have been no previos mpg's to calc. the second time around (all this is in a while loop, btw) and thereafter it should always flow into the else. i just found it cumbersome and inefficent. i was wondering if there was a more clever algorithm. thanks for the help :)
• 04-03-2002
salvelinus
In that case, do the first calculation before you enter the loop. Subsequent calculations can be done in the loop. You shouldn't need to check i then, won't need an if.
Based on the code you posted, maybe other stuff in your program would still require it.