Difference in code
Hi guys. I wrote some code and expected the result to be the same... Can't figure out why it is not. Can anyone pls explain it to me??
1 2 3 4 5 6 7 8
|
int square(int l)
{
int w = l;
for (int y = 1; l > y; ++y) {
w += w;
}
return w;
}
|
1 2 3 4 5 6 7 8
|
int square(int l)
{
int w = 0;
for (int y = 0; l > y; ++y) {
w += l;
}
return w;
}
|
Last edited on
In the first code you are adding w that is changing, in the second code you are adding l that is NOT changing.
Topic archived. No new replies allowed.