# Begginer's Loop Questions

Hey all, I'm new to this C++ thing and I've run across this program in my book which takes in five values and returns their sum and average. It's written as such:

****************************************
double number;
double sum=0.0;
for (int i=1; i <= 5; i++)
{
cout << "Enter Value " << i << " : ";
cin >> number;
sum += number;
} //block ends here
cout << "
These numbers sum to: " << sum;
cout << "
These numbers average: " << sum/5;
***********************************************

I have two questions regarding this program.

1) How does the "sum +=number" line work? I understand the += concept, however, what I don't understand is how this line tells the computer to store each number and add all 5 of them together at the end. Unless I'm mistaken as to the operation of +=, this is not something that the operator should do. Yet it does.....

2) Let's say I rewrite the loop command line to read:

for (int i=1; i = 5; i++) //note the change of i=5....

Shouldn't this loop terminate as soon as the expression is false (i.e. shouldn't the loop not run at all, because the beggining i=1?)

Yet, when I run the program, I get stuck in this infinite loop of the program asking me to "Enter Value 5:". What is wrong with my thinking here?

Thanks!

• : Hey all, I'm new to this C++ thing and I've run across this program in my book which takes in five values and returns their sum and average. It's written as such:
:
: ****************************************
: double number;
: double sum=0.0;
: for (int i=1; i <= 5; i++)
: {
: cout << "Enter Value " << i << " : ";
: cin >> number;
: sum += number;
: } //block ends here
: cout << "
These numbers sum to: " << sum;
: cout << "
These numbers average: " << sum/5;
: ***********************************************
:
:
: I have two questions regarding this program.
:
: 1) How does the "sum +=number" line work? I understand the += concept, however, what I don't understand is how this line tells the computer to store each number and add all 5 of them together at the end. Unless I'm mistaken as to the operation of +=, this is not something that the operator should do. Yet it does.....
:
:
:
: 2) Let's say I rewrite the loop command line to read:
:
: for (int i=1; i = 5; i++) //note the change of i=5....
:
: Shouldn't this loop terminate as soon as the expression is false (i.e. shouldn't the loop not run at all, because the beggining i=1?)
:
: Yet, when I run the program, I get stuck in this infinite loop of the program asking me to "Enter Value 5:". What is wrong with my thinking here?
:
: Thanks!
:
1)
Each time the loop is entered, the user supplies a number which is stored in the variable named number.
[code]
cin>>number;
[/code]
Then the number is added to the sum of the previous entered numbers which is stored in the variable sum. The result is stored in the variable sum.
[code]
sum+=number;
[/code]
In fact, the addition is made at each step. The 5 numbers are not summed at the end.

2)
Use == for comparison. The statement 'i=5' assigns 5 to i and the whole statement has value 5, which is considered to be true. This explains why the loop continues infinitely (each time, i is incremented but then in the test part it is assigned value 5).

Steph
• Ahhhh, I get it now, the values aren't stored, but the value is constantly updated with each occurence of the loop. Thanks, you explained it perfectly.

: : Hey all, I'm new to this C++ thing and I've run across this program in my book which takes in five values and returns their sum and average. It's written as such:
: :
: : ****************************************
: : double number;
: : double sum=0.0;
: : for (int i=1; i <= 5; i++)
: : {
: : cout << "Enter Value " << i << " : ";
: : cin >> number;
: : sum += number;
: : } //block ends here
: : cout << "
These numbers sum to: " << sum;
: : cout << "
These numbers average: " << sum/5;
: : ***********************************************
: :
: :
: : I have two questions regarding this program.
: :
: : 1) How does the "sum +=number" line work? I understand the += concept, however, what I don't understand is how this line tells the computer to store each number and add all 5 of them together at the end. Unless I'm mistaken as to the operation of +=, this is not something that the operator should do. Yet it does.....
: :
: :
: :
: : 2) Let's say I rewrite the loop command line to read:
: :
: : for (int i=1; i = 5; i++) //note the change of i=5....
: :
: : Shouldn't this loop terminate as soon as the expression is false (i.e. shouldn't the loop not run at all, because the beggining i=1?)
: :
: : Yet, when I run the program, I get stuck in this infinite loop of the program asking me to "Enter Value 5:". What is wrong with my thinking here?
: :
: : Thanks!
: :
: 1)
: Each time the loop is entered, the user supplies a number which is stored in the variable named number.
: [code]
: cin>>number;
: [/code]
: Then the number is added to the sum of the previous entered numbers which is stored in the variable sum. The result is stored in the variable sum.
: [code]
: sum+=number;
: [/code]
: In fact, the addition is made at each step. The 5 numbers are not summed at the end.
:
: 2)
: Use == for comparison. The statement 'i=5' assigns 5 to i and the whole statement has value 5, which is considered to be true. This explains why the loop continues infinitely (each time, i is incremented but then in the test part it is assigned value 5).
:
: Steph
:

• [b][red]This message was edited by Lundin at 2006-5-29 23:31:14[/red][/b][hr]
: Hey all, I'm new to this C++ thing and I've run across this program in my book which takes in five values and returns their sum and average. It's written as such:
:
: ****************************************
: double number;
: double sum=0.0;
: for (int i=1; i <= 5; i++)
: {
: cout << "Enter Value " << i << " : ";
: cin >> number;
: sum += number;
: } //block ends here
: cout << "
These numbers sum to: " << sum;
: cout << "
These numbers average: " << sum/5;
: ***********************************************
:
:
: I have two questions regarding this program.
:
: 1) How does the "sum +=number" line work? I understand the += concept, however, what I don't understand is how this line tells the computer to store each number and add all 5 of them together at the end. Unless I'm mistaken as to the operation of +=, this is not something that the operator should do. Yet it does.....

[blue]
sum += number
is equal to
sum = sum + number

So every time you run that line, the number gets added to the sum.

[/blue]

:
: 2) Let's say I rewrite the loop command line to read:
:
: for (int i=1; i = 5; i++) //note the change of i=5....
:
: Shouldn't this loop terminate as soon as the expression is false (i.e. shouldn't the loop not run at all, because the beggining i=1?)
:
: Yet, when I run the program, I get stuck in this infinite loop of the program asking me to "Enter Value 5:". What is wrong with my thinking here?
:
: Thanks!
:

[blue]Note that you are using = (assignment) and not == (equal).
If you are lucky, the compiler should give you a warning when assigning a value inside a condition. Otherwise the following will happen:

First i gets set to the value 1.
Then the loop condition is checked. There it will find the code "set i to the value 5" which it will execute. Then it evaluates the condition to see if it is true or false. It will be the same as if writing

if(5)
{
}

which is always true.

Then the program runs the code in the loop.
It increases i by 1. i has the value 6 for a short moment.
And then it checks the loop condition again, where it will
assign the value 5 to i again. And we have an eternal loop.
[/blue]

• : Hey all, I'm new to this C++ thing and I've run across this program in my book which takes in five values and returns their sum and average. It's written as such:
:
: ****************************************
: double number;
: double sum=0.0;
: for (int i=1; i <= 5; i++)
: {
: cout << "Enter Value " << i << " : ";
: cin >> number;
: sum += number;
: } //block ends here
: cout << "
These numbers sum to: " << sum;
: cout << "
These numbers average: " << sum/5;
: ***********************************************
:
:
: I have two questions regarding this program.
:
: 1) How does the "sum +=number" line work? I understand the += concept, however, what I don't understand is how this line tells the computer to store each number and add all 5 of them together at the end. Unless I'm mistaken as to the operation of +=, this is not something that the operator should do. Yet it does.....
:
:
:
: 2) Let's say I rewrite the loop command line to read:
:
: for (int i=1; i = 5; i++) //note the change of i=5....
:
: Shouldn't this loop terminate as soon as the expression is false (i.e. shouldn't the loop not run at all, because the beggining i=1?)
:
: Yet, when I run the program, I get stuck in this infinite loop of the program asking me to "Enter Value 5:". What is wrong with my thinking here?
:
: Thanks!
:
For your first question, Sum += Number;
This statement will add Number to Sum. Sum is initialized as 0 at the start of the program, so if you add 5 to it, it becomes 5 (Sum += 5;)
If later in the program (or in this case the 2nd iteration of the for loop) you decide to add a second number 9, it adds 9 to the 5 to make 14. This continues to add the numbers together until all 5 numbers are given in the for loop.

As for the second question, the for loop takes 3 arguments.
One to say what the first iteration is (i = 1;), another to say keep looping over as long as this condition is true (i <= 5;) and what to do in every iteration of the loop (i++) But what you're saying here is to keep looping as long as (i = 5) personally I'm amazed this doesn't return a compile error because if anything, this should read (i == 5;) The loop begins with i at 1, the loop continues as long as i = 5.

Ah ha! I have just realised it. This won't return a compile error because it's perfectly legal to have (i = 5;) but this doesn't do what you think. You are assigning the value 5 to i! Not checking to see if i equals 5. That is why you are getting an endless loop with i equal to 5, which explains why you are getting "Enter Value 5:" all the time.

If you put in (i == 5;) rather than (i = 5;) I would think you'd get an endless loop with i equal to 1. Or maybe nothing at all.

The reason for this endless loop is because you haven't put the condition in for the loop to run, you've actually written the statement to assign the 5 to i, this second argument must be a condition check otherwise I believe the compiler will assume you want an endless loop. The same will happen with while(1).