int sum=0, a=0, b=0;
for (a=1;a<3;a++)
for (b=1;b<5;b++)
sum+=b;
cout <<"sum =" <<sum;

The answer is 20, sure, but I have'nt got my head around how it actually works.

Could someone explain it step by step please?

Do you know what the for loop or the += operator is doing?

//have it set to int where it is just integers (whole numbers)
int sum=0, a=0, b=0; //shows that your sum of both values, first //value (a), second value (b) is set to zero.
//you are using a for loop
for (a=1;a<3;a++) //with this for loop, you have you first value set //to 1 but is less than three, and since it is, 
//you increment the value each time
for (b=1;b<5;b++)// same here except your value less than five, //will increment each time
sum+=b;// you basically saying sum = sum + b which is the same //as sum += b;
cout <<"sum =" <<sum;//output statement of what the sum equals //to and display

return 0;

}

//I hope this helps! Let me know if it doesn't. you can shoot me //and email <snipped>, I answer frequently

> I'm writing exactly the same code, but probably a bit easier to understand:

>> Original posted code:

int sum=0, a=0, b=0;
for (a=1;a<3;a++)
for (b=1;b<5;b++)
sum+=b;
cout <<"sum =" <<sum;

>> The above code is actually pretty much the same as this :):

int sum=0, a=0, b=0;
for (a=1;a<3;a++)
     for (b=1;b<5;b++)
          sum+=b;
cout <<"sum =" <<sum;

>>> Line 1: int sum=0, a=0, b=0; declare variables (variables are used to store data in, a variable is actually a name (assigned by the programmer) for a place in the computer's memory

>>> Line 2: for (a=1;a<3;a++) , a for-statement which is executing three times the following instruction: for (b=1;b<5;b++) (another for-statement), a for-statement is called a 'loop' which means it's repeating instructions until a certain condition isn't true anymore ...

>>> Line 3: sum+=b; , this means the same as sum = sum + b; which means that the variable 'sum' is incremented by the value of variable 'b' each time ...

>>> Line 4 (the last instruction): cout <<"sum =" <<sum; , cout is an object from the C++ Class Library and is used to display things on the screen, when you run this code, the output you see on your screen is what this line of code did ...

>>> You can find some more information on the for-loop here ...

> Hope this helps :P !

> By the way, what don't you understand? We can't give precise information if you aren't explaining where you're specifically having problems with ...

i think there is more to observe than to explain in here . i guess it would be prudent if you could run the underneath code and observe the output , it would be crystal clear .
The code does the same job :

#include<iostream>
using namespace std;

int main ()
{
    int sum=0, a=0, b=0;
for (a=1;a<3;a++)
{
    cout<<"start of b loop"<<endl;
        for (b=1;b<5;b++)
    {

    cout<<sum<<"+"<<b<<"=";
    sum+=b;
    cout<<sum<<endl;
    }
    cout<<"end of b loop "<<endl;
}
cout <<"the final sum is =" <<sum;
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.