# how to improve

This is a discussion on how to improve within the C++ Programming forums, part of the General Programming Boards category; anybody wanna take a look and see if this can be streamlined anymore. the mpg needs to be cumulative, can ...

1. ## 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++;```

2. I think you want if (i==0) instead of if(i=0)

3. ## Re: how to improve

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++;```

4. ## Re: how to improve

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.

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

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