It's not just your loop condition that's incorrect... your whole loop logic is a little messed up.
while loops are very simple:
1 2 3 4
|
while( condition )
{
body;
}
|
This will do the following:
1) Check the Condition. If it's false, the rest of the loop is skipped and your program continues on.
2) Otherwise, if the condition is true, perform the 'body'
3) After the body is performed, go to step #1 and check the condition again.
That's it.
Your condition here doesn't make much sense:
while (sum <= increment)
Since 'increment' is going to be added to sum each time the loop body is run... this means the loop body will run
at most 1 time.
Your body also doesn't make much sense (and your comments do not reflect what is actually happening). It's as if you are mixing up your variables.
Here is your code again just for quick reference:
1 2 3 4 5
|
while (sum <= increment)
{
sum = start + increment;
sum = sum + increment;
}
|
Now let's walk through this line by line to see what is happening. Let's start by using the numbers you plugged in:
start = 8
end = 121
increment = 17
sum = 0 (you initialize it to zero)
1) Condition is checked
(sum <= increment)
becomes
(0 <= 17)
. This is true... so the loop body executes
2)
sum = start + increment;
start=8 and increment=17... 8+17=25. So after this line of code...
sum=25
3)
sum = sum + increment;
sum=25 (from step 2) and increment=17. 25+17=42. So after this line of code...
sum=42
4) Loop body is complete, so check the condition again.
(sum <= increment)
becomes
(42 <= 17)
. This is false... so the loop is complete. The loop body is not executed again.
A few things to note about this:
- You are not using 'end' anywhere... so the value the user inputs for it is completely ignored.
- You are initializing 'sum' with 0 (this is probably wrong).