For Loop Execution Steps

Hello everyone,

I am a C++ beginner and this is not a homework question but more for my general understanding. I understand how pre-increment works and how post increment works, but I am having trouble grasping the execution steps from the code below (which is from the internet taken).
Ask: Write a program to find the sum of first n natural numbers where n is entered by user.
For loop steps:
1 - user puts in how many numbers he wants to find out the sum to (Input: 3)
2 - the test expression is true thus the program moves to the body of the code
3 - so sum is already set to 0 and count will be set to 1 so sum will be 1
4 - the ++count comes into place which increments 1 to 2
5 - 2 is true and we move to the body of the code
6 - (not sure if all the above steps are correct but here is my confusion) sum is set to 1 and count is set to 2 thus sum will be 3?
7 - ?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
	{
    int n;
	int count;
	int sum=0;
    cout <<"Enter the value of n ";
    cin >> n;
    for(count=1;count<=n;++count)  //for loop terminates if count>n
    {
        sum+=count;    /* this statement is equivalent to sum=sum+count */
    }
    cout << "The sum is: "<< sum<<endl;
	system ("pause");
    return 0;
}


Thank you everyone for your help and appreciate the patience :)
Last edited on
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
Last edited on
Hello,

Thank you for the detailed explanation and I also fixed the code! It makes sense!!!
I got carried away with the increment.

Much appreciated! :)
Happy to help. Goodluck :)
Topic archived. No new replies allowed.