# For loop

• 07-22-2011
arbitel
For 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];`
Why is there a sum=a[i][j] in the for loop ? Is it initiated every single time or how does this work ??

Thanks,

Marcus
• 07-22-2011
manasij7479
Quote:

Is it initiated every single time or how does this work ??
NO, it isn't .
• 07-22-2011
arbitel
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]; }```
or
Code:

```sum=a[i][j] for (k=i-1; k>=0; k--) { sum -= a[i][k]; }```
• 07-22-2011
AndrewHunter
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-2011
AndrewHunter
If you are new to for loops, take a look at cprogs tutorial on loops
• 07-22-2011
arbitel
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-2011
iMalc
Quote:

Originally Posted by arbitel
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.