Hey and welcome to the forum. Please use code-tags for all of your code -
http://www.cplusplus.com/articles/jEywvCM9/
The program is made to add each count together and put them in the sum. For example, as the step 1 says. we input 3. So
n = 3;
This is the for loop in question.
for(count=1;count<=n;++count)
count=1;
This part inside the for-loop parameters (condition) has a variable called count and we assign it the number 1. This is so we can tell the for-loop the amount of times it has to go around (iterate);
count<=n;
This part tells the for-loop how man rounds to go. This says, run this for loop as long as n is bigger or equal to count. Basically until count is equal to 3.
++count)
Last part of the for-loop condition. This will tell the for-loop by how much
count
will increase each iteration (round). Count starts at 1. And after a full loop, it increases by 1, thats what the ++ does. If you wanted count to increase by 2 each round, you would do
count+=2
Inside the for loop one thing happens.
this -
sum+=count;
What you do in here is. Sum is equal to 0 to begin with, you initialized it that way. int
sum=0;
The for-loop will run 3 times since we entered n to be 3. Count starts at 1. So after 1 round sum will be equal to 1. Because sum+=count is another way of saying sum = sum + count.
So during the first round we have 0 = 0+1. As you can see it's equal to 1.
Second round. Count is equal to 2, because it's the second round and we increased it by one using
++count
in the for loop condition.
So now we have 1 = 1 + 2. Sum is now 3.
Last round, count is 3. We get 3 = 3 + 3. Sum is equal to 6. It's been 3 rounds so we exit the for-loop and find this
cout << "The sum is: "<< sum<<endl;
Which prints out the value of the sum, which happens to be 6.
I tried my best at explaining hope it helps. There is tons of information on everything on google. I also recommend these 2 c++ tutorial playlists on youtube. They got tutorials on all the basics of c++, all kinds of loops etc.
C++ Made Easy -
https://www.youtube.com/playlist?list=PL2DD6A625AD033D36
Bucky -
https://www.youtube.com/playlist?list=PLAE85DE8440AA6B83