Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# for loop problem

Greenhorn
Posts: 5
1
First of all, hello to everyone.

Would you be so kind and help me with this problem.

The result should be 91, but I get 105. The loop goes once more.
Thanks.

hj

Rancher
Posts: 2767
32
Try debugging the code in the loop by adding a print statement first thing in the loop that prints the value of sum and i.
The print out should help you see what is happening so you can change the logic to get the desired results.

Bartender
Posts: 1445
30
Welcome to the Ranch, Hrvoje.

What makes you think the final result should be 91? Note that, at Line 9, the loop will continue each time sum is less than max. Well, if sum were 91, it would be less than 100, wouldn't it? That means the loop will continue for another pass. Line 10 will print the value of i (13, I believe), then i will increase by 1 (to 14, I believe), then, at Line 8, i will again be added to sum. At which point, what value will sum have? And, having that value, what will happen when Line 9 next executes?

Java Cowboy
Sheriff
Posts: 16080
88
Hrvoje, can you explain what the code is supposed to do? It's hard to point out the error if we don't know what exactly the code should calculate.

Hrvoje Jug
Greenhorn
Posts: 5
1

Jesper de Jong wrote:Hrvoje, can you explain what the code is supposed to do? It's hard to point out the error if we don't know what exactly the code should calculate.

Sorry, my question wasn't clear.

Write a program that prints the numbers from 1 to N with the condition that their sum is less than a constant value SUM_MAX.
So the answer should looks like this:
Enter N: 20
Enter sum: 100
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
Sum of numbers from 1 do 13 is 91 ( < 100 ).

My problem is that I don't wont that final pass.
When the "sum = sum + i" is before if statement I got 1, ... 13 and that is OK (when I put it after if statement --> 1, ... 14 which is not OK).
But, the loop passes once more and at the end I've got 105 which is wrong, because the condition (sum < sum_max) is not OK. I can't put the result "sum" into the loop, I need it at the end.

Thanks to all.
Hrvoje

Bartender
Posts: 1735
43
Can you explain the condition sum < max in your code ? I think you mean (sum + i > max) instead

Hrvoje Jug
Greenhorn
Posts: 5
1
• 1

salvin francis wrote:Can you explain the condition sum < max in your code ? I think you mean (sum + i > max) instead

Thank you so much. I can't explain the condition (sum < max), it was wrong. I've missed "+ i", it should be (sum + i > max) and it works.

Final code is:

Thanks again.
Hrvoje

Bartender
Posts: 10575
66
• 1

Hrvoje Jug wrote:Final code is:...

Well done! But you still have a small mistake, and you can also save yourself quite a bit of typing:
I don't know if you've seen the conditional operator ('? :') yet; but it's worth getting to know.

Also: What happens if your user enters '0' or '1' (or indeed '-53') for either N or SUM_MAX?

HIH

Winston

Hrvoje Jug
Greenhorn
Posts: 5
1

Winston Gutkowski wrote:

Hrvoje Jug wrote:Final code is:...

Well done! But you still have a small mistake...

Thanks for the compact code.

BR,
Hrvoje

Winston Gutkowski
Bartender
Posts: 10575
66

Hrvoje Jug wrote:Thanks for the compact code.

You're welcome, but you still have the same mistake. Try entering n=10 and sum=62 and see what happens.

Winston

Hrvoje Jug
Greenhorn
Posts: 5
1

Winston Gutkowski wrote:

Hrvoje Jug wrote:Thanks for the compact code.

You're welcome, but you still have the same mistake. Try entering n=10 and sum=62 and see what happens.

Winston

Thanks. This one?

You've shown me, but I forgot to change.
BR,
Hrvoje.

Winston Gutkowski
Bartender
Posts: 10575
66

Hrvoje Jug wrote:You've shown me, but I forgot to change.

But the most important thing is to understand why it's wrong.

My suggestion: leave the code as it is and try out the values I suggested; then maybe add a couple of extra debug print() statements to find out why it prints the wrong values. That'll help you to avoid similar mistakes in the future.

Winston

 The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?" The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS