Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
December 12th, 2012, 04:28 AM
 electriceel
Registered User

Join Date: Sep 2012
Posts: 14
Time spent in forums: 2 h 56 m 35 sec
Reputation Power: 0
For loop problem

I have a strange problem when using a for loop:
Code:
for (int i = 0; i < NUMBER_OF_JOBS; i++, SUM += JOBS[i].getWeight()*JOBS[i].getLength())
Code:
for (int i = 0; i < NUMBER_OF_JOBS; i++)
{
SUM += JOBS[i].getWeight()*JOBS[i].getLength();
}

after fiddling around I discovered that it was because the former failed to add the first summand (i.e. when i == 0) into the SUM. What is the reason for this?

#2
December 12th, 2012, 04:40 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,261
Time spent in forums: 2 Months 4 Weeks 1 Day 15 h 13 m 24 sec
Reputation Power: 1827
Code:
for (int i = 0;
i < NUMBER_OF_JOBS;
i++, SUM += JOBS[i].getWeight()*JOBS[i].getLength())

It's because the i++ happens BEFORE you do the sum, when you write the loop like this.

Written normally, the i++ is the last thing that happens in the loop.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

#3
December 12th, 2012, 08:09 AM
 electriceel
Registered User

Join Date: Sep 2012
Posts: 14
Time spent in forums: 2 h 56 m 35 sec
Reputation Power: 0
Quote:
 Originally Posted by salem Code: for (int i = 0; i < NUMBER_OF_JOBS; i++, SUM += JOBS[i].getWeight()*JOBS[i].getLength()) It's because the i++ happens BEFORE you do the sum, when you write the loop like this. Written normally, the i++ is the last thing that happens in the loop.

Thanks for the reply, but then why does
Code:
int j = 0;
for (int i = 0; i < 10; i++, j+=1)
produce the correct output of j = 10?

#4
December 12th, 2012, 09:07 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,261
Time spent in forums: 2 Months 4 Weeks 1 Day 15 h 13 m 24 sec
Reputation Power: 1827
> produce the correct output of j = 10?
Because j doesn't depend on i.

Whereas your previous case, i was used as a subscript, so the second expression was dependent on i.

#5
December 12th, 2012, 09:49 AM
 electriceel
Registered User

Join Date: Sep 2012
Posts: 14
Time spent in forums: 2 h 56 m 35 sec
Reputation Power: 0
Quote:
 Originally Posted by salem > produce the correct output of j = 10? Because j doesn't depend on i. Whereas your previous case, i was used as a subscript, so the second expression was dependent on i.

Ah, I get what you mean now, I confused there because I thought that the summation has simply lost the first term, but it has gained an extra last term. Just one last question, this last term - it is out of bounds - so is it because of the default constructor or something that it happens to be zero?

#6
December 12th, 2012, 10:11 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,261
Time spent in forums: 2 Months 4 Weeks 1 Day 15 h 13 m 24 sec
Reputation Power: 1827
It's out of bounds, so anything can happen.
Today, you got zero.
Tomorrow, you might get some random bit of garbage.
Next week, a segmentation fault for an illegal memory access.

#7
December 13th, 2012, 01:40 AM
 electriceel
Registered User

Join Date: Sep 2012
Posts: 14
Time spent in forums: 2 h 56 m 35 sec
Reputation Power: 0
Quote:
 Originally Posted by salem It's out of bounds, so anything can happen. Today, you got zero. Tomorrow, you might get some random bit of garbage. Next week, a segmentation fault for an illegal memory access.

Got it, thanks!

 Viewing: Dev Shed Forums > Programming Languages > C Programming > For loop problem