Hi I am not quite sure what this for loop means
Why is there a sum=a[i][j] in the for loop ? Is it initiated every single time or how does this work ??Code:for (sum=a[i][j], k=i-1; k>=0; k--) sum -= a[i][k];
Thanks,
Marcus
Printable View
Hi I am not quite sure what this for loop means
Why is there a sum=a[i][j] in the for loop ? Is it initiated every single time or how does this work ??Code:for (sum=a[i][j], k=i-1; k>=0; k--) sum -= a[i][k];
Thanks,
Marcus
NO, it isn't .Quote:
Is it initiated every single time or how does this work ??
Can you explain more on that ? Sorry I am new to C++.
So would it be
orCode:for (k=i-1; k>=0; k--) {
sum=a[i][j]
sum -= a[i][k];
}
Code:sum=a[i][j]
for (k=i-1; k>=0; k--) {
sum -= a[i][k];
}
And to programming.....whoever wrote this line of code was just being lazy and decided to keep everything on the same line. That line of code is equal to:
Code:
sum = a[i][j];
for(k= i-1; k > 0; k--){
sum = sum - a[i][k];
}
If you are new to for loops, take a look at cprogs tutorial on loops
Thanks. The reason I was confused was because the sum=a[i][j] in the for loop. Usually for loop only has a intialization, condition and increment but not the first term. Anyways, thanks !
It does. The trick is that these are separated by semicolons.
The bit before the first semicolon runs once, at the beginning.
The bit between the semicolons is the continuation condition and it runs at the start of every loop.
The part after the second semicolon is the incrementing condition and is run at the end of every iteration of the loop.
What you had was two pieces of code in the initialisation part of the for-loop, which is possible when using the comma operator.