This discussion is archived
4 Replies Latest reply: Oct 25, 2007 7:11 AM by 807600

# Logic of this Algorithm.... IT MAKES NO SENSE!!!

Currently Being Moderated
``````int sum=0
int next = input("Input first number: ");
while(NEXT>0)      //start control loop
{
sum = sum+next   //add it in
next = inputInt("Input next value: ");   // get next value
}
output("the sum is: " +sum); //display result``````
What would happen if you were to enter in the values: 14, 54, 7, 0, 23, 12, -1

Here is an example of what would happen f you used 4, 78, 2, -2, 4, 0, 9

Next Next>0 Sum Output explaination
4 - 0 - initial conditions
4 true 4 - first time round the first loop
78 true 4 - -
2 true 82 - -
-2 false 84 - terminates

Please help. The example was given off a book, but no one can understand it.... can you???
• ###### 1. Re: Logic of this Algorithm.... IT MAKES NO SENSE!!!
Currently Being Moderated
if first input returns 4 then
next = 4
next > 0 == true
sum = 4

second input 78
next = 78
next > 0 == true
sum = 82

third input 2
next = 2
next > 0 == true
sum = 84

fourth input -2
next = -2
next > 0 == false
now it wount go into the while loop again so sum will be 84
• ###### 2. Re: Logic of this Algorithm.... IT MAKES NO SENSE!!!
Currently Being Moderated
that's what i thought at first... look at it again. that's not what it is suppose to be.
• ###### 3. Re: Logic of this Algorithm.... IT MAKES NO SENSE!!!
Currently Being Moderated
java is case-sensitive....
• ###### 4. Re: Logic of this Algorithm.... IT MAKES NO SENSE!!!
Currently Being Moderated
scurvyknave wrote:
that's what i thought at first... look at it again. that's not what it is suppose to be.
After your last thread, I suppose it's pointless to ask this, but what specific problem are you having or what specifically don't you understand?