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

- 07-22-2011arbitelFor loop
Hi I am not quite sure what this for loop means

Code:`for (sum=a[i][j], k=i-1; k>=0; k--) sum -= a[i][k];`

Thanks,

Marcus - 07-22-2011manasij7479Quote:

Is it initiated every single time or how does this work ??

- 07-22-2011arbitel
Can you explain more on that ? Sorry I am new to C++.

So would it be

Code:`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];

}

- 07-22-2011AndrewHunter
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];

}

- 07-22-2011AndrewHunter
If you are new to for loops, take a look at cprogs tutorial on loops

- 07-22-2011arbitel
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 !

- 07-22-2011iMalc
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.