This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can someone explain loops to me?  RSS feed

 
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Soooo...the one problem in my book is to create a loop where you add the integers from 1 to 50. I somehow did it correctly but honestly don't know
what it actually means. Can someone please explain what the parts in the loop mean or point me in the right direction?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

here you take i as counter which starts from 0 and counts up to max i.e. 50

every time loop executes for value of i (i.e. from 0 to 50), sum field is assigned its value added with i.
for example say at i = 0, sum = 0, then sum = 0 + 0 = 0;
again i = 1, sum = 0, then sum = 1 + 0 = 1;
and so on ....

Best way to know how your program works, is to use debugger....
 
Java Cowboy
Sheriff
Posts: 16082
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Oracle's tutorial: The for Statement
 
Bartender
Posts: 2155
46
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ryan Bishop wrote:Soooo...the one problem in my book is to create a loop where you add the integers from 1 to 50. I somehow did it correctly (...)



Well.. You did not do that correctly. You said you wanted to add integers from 1 to 50. And you added integers from 0 to 50. I know that mathematically this is the same...
But programatically it's incorrect. If you wanted multiplication instead of the sum you would get an incorrect value.
I'm not just nitpicking for no good reason. you should be careful with boundaries in loops as you might get yourself a serious and hard to spot bug.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ryan Bishop wrote:Can someone please explain what the parts in the loop mean or point me in the right direction?


Well, in logical terms, your for loop is precisely equivalent to the following:Not sure if it helps, but sometimes its good to see things written differently.

Bhawana Gupta wrote:Best way to know how your program works, is to use debugger...


Actually, I'm not sure that's true. IMO, the best way to know how your program works - especially when you're starting out - is to sit down with a paper and pencil and write it out.

HIH

Winston
 
lowercase baba
Bartender
Posts: 12613
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better indentation might help...


This makes it easier to see what exactly is the loop and what isn't...Your loop consists of only lines 8-10. So focus on that, and ask specific questions if it doesn't make sense.
 
Ranch Hand
Posts: 225
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Ryan Bishop wrote:Can someone please explain what the parts in the loop mean or point me in the right direction?


Well, in logical terms, your for loop is precisely equivalent to the following:Not sure if it helps, but sometimes its good to see things written differently.

Bhawana Gupta wrote:Best way to know how your program works, is to use debugger...


Actually, I'm not sure that's true. IMO, the best way to know how your program works - especially when you're starting out - is to sit down with a paper and pencil and write it out.

HIH

Winston


The only difference between I would have written this than Winston is
.

And, for what it's worth, I second the pencil and paper. It's amazing how clear things become when you start thinking about *every* step you need to take to accomplish a task.

Regards,
Robert
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!