• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Why 45?

 
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am currently working with a for loop and I have most of the functionality figured out.

Now this is not a big thing I am experimenting with an example from a book.

Why does this program return the answer 45 when I run it?

//Br F

 
Rancher
Posts: 4904
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why does this program return the answer 45


What value do you expect to be printed?  
Have you desk checked the code to see what the correct output should be?
 
Fredrik Andlin
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is probably correct I am trying to under why it comes to that result. The first part of the for loop adds 1 to 0 and so on until it reaches 10. Then it moves on to the next part wich is "total+=i" and I think that is the part I have not yet figured out since this is an example were the point is to understand why it returns the value 45.

Norm Radder wrote:

Why does this program return the answer 45


What value do you expect to be printed?  
Have you desk checked the code to see what the correct output should be?

 
Saloon Keeper
Posts: 9834
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many times will line 9 be executed? What will the value of 'i' be each time?
 
Norm Radder
Rancher
Posts: 4904
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To see what the code is doing, add a print statement that prints the value of i and the value of total for each iteration of the loop.
The print out will show you what is happening.
 
Fredrik Andlin
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, thank you now I understand what actually happened. Good clue from you.

Carey Brown wrote:How many times will line 9 be executed? What will the value of 'i' be each time?

 
Fredrik Andlin
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Was the first thing I tried but now I understand what happened thanks for the support.

Norm Radder wrote:To see what the code is doing, add a print statement that prints the value of i and the value of total for each iteration of the loop.
The print out will show you what is happening.

 
Marshal
Posts: 77197
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fredrik Andlin wrote:The answer is probably correct

45? As I have been singing recently, “...all is quite correct.”

. . . until it reaches 10. . . .

Have you worked out how many times the loop will run yet? Have you found out whether it reaches ten?
 
lowercase baba
Posts: 13086
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When just starting out, one of your BEST friends is "System.out.println".

When something isn't working like you'd expect, throw some of those bad boys in.  If "total" isn't what you think it should be, print out its value every time it changes. Using it is a great way to see what's happening beneath the cover, so to speak.
 
Campbell Ritchie
Marshal
Posts: 77197
370
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you have worked out how your loop works, delete that class, or at least rename it and delete the Math.class file. You will have no end of confusion in a few weeks because you have written a class with the same name as another in the java.lang package.
Remove the import because you aren't using it.
 
Campbell Ritchie
Marshal
Posts: 77197
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since OP seems to have gone away, and somebody else might come across this thread, I shall let the cat out of the bag. Because the loop includes i < 10 (line 8), it will run until i equals 9 and then stop. The sum of natural numbers 0...9inc is 45.
 
reply
    Bookmark Topic Watch Topic
  • New Topic