Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

For loop question

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! I am new to Java and to this forum. To be honest, I am rather new to programming as a whole. A while back I got into a bit of webbuilding which lured me to using some PHP. Now I actually liked the puzzling. So I figured I might go a bit deeper. I am now learning Java from a book and got a android app course lined up (making loads of tiny programs to practice ofcourse;)) Still sometimes there is something I do not fully understand. So hence my registration on this forum. Hopefully folks here can help me and I hope I can also help out the newerbees coming after me;)

My first question! The code I post underneath comes from the book I use. This code lists all the possibilities of Cherry, lemon, Kumquat and rutabaga. Now, what I do not get: How does the computer know I want all the combinations? Why does it not simply print the same combi "cherry, lemon, kumquat, rutabaga" and gets it over with? I do not see what piece of code tells the pc to repeat until all possible combinations are done. Can someone shed some light on this for me please?



 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Diederik Lucas wrote:My first question! The code I post underneath comes from the book I use. This code lists all the possibilities of Cherry, lemon, Kumquat and rutabaga. Now, what I do not get: How does the computer know I want all the combinations? Why does it not simply print the same combi "cherry, lemon, kumquat, rutabaga" and gets it over with? I do not see what piece of code tells the pc to repeat until all possible combinations are done. Can someone shed some light on this for me please?


Erm ... because that's what you've told it to do.

You don't have one loop, you have 3 - one inside the other, and for each execution of the inner loop you print out all three "reel" values.

My suggestion: Have a think how you would ask a computer to print out all possible combinations of three rolled dice, because that's essentially what you're doing here, except that your "dice" have 4 values, not 6; and they have names, not numbers.

Winston
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is exactly telling the computer "execute code below for all possible Symbols:


So if you combine three of those you'll get all possible combinations.
 
Marshal
Posts: 75644
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags to your post, which you should always use, and don't they make it look better
 
Dirk Lucas
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aaahhh now I see. So it says for each iteration print each possible value for left, middle and left.

So (Symbol leftReel : Symbol.values() = for each Symbol leftReel print out each possible symbol value ---> I get it.

Thank you both for answering my newbish question;)


Ty for welcome, I will do that from now on!
 
lowercase baba
Posts: 13048
67
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Diederik Lucas wrote:So (Symbol leftReel : Symbol.values() = for each Symbol leftReel print out each possible symbol value ---> I get it.


I wouldn't quite put it that way...remember, programmers tend to be quite literal.

What says is "for every possible symbol you can get from the Symbol class...<do something>"

what that <do something> does is irrelevant to this line. it may print stuff. It may add money (or for a slot machine take away money). It may compute an integral...it could do anything, but the only way to know is to look at what is inside the curly braces.

In this case what happens to be inside it is a loop, and that one actually has yet ANOTHER loop inside it.

I think of them as old mechanical odometers on cars. The right wheel/inner most loop runs all the way around, hitting every value. then you change the center/middle loop by one click, and the inner most wheel goes all the way around again, the second wheel clicks over one...etc.

 
Dirk Lucas
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok with some output I think I got it now:

cherry cherry cherry
cherry cherry lemon
cherry cherry kumquat
cherry cherry rutabaga
cherry lemon cherry
cherry lemon lemon
cherry lemon kumquat
cherry lemon rutabaga
cherry kumquat cherry
cherry kumquat lemon
cherry kumquat kumquat
cherry kumquat rutabaga
cherry rutabaga cherry
cherry rutabaga lemon

The {...} Symbol Leftreel makes Symbol a value like an int or double. So the variable leftReel can be any of the symbols so if I give this line it will display all values. Since its a loop in a loop in a loop its starts with the most innerloop changing the value all the time than it goes to the loop outside this one and so on and like mentioned you get all combinations.

I do try to rollplay a computer when reading code. Still. Its an art to unerstand it fully if you ask me;)
 
fred rosenberger
lowercase baba
Posts: 13048
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It starts with line 1 - the outermost loop. it creates a variable named "leftReel" that is of type Symbol. It then sets that variable to the first element of the Symbol enum, which is "cherry". You could add a print method between the above line 1 and 2 to prove this.

Once it does that, it goes to line 2, and does the same thing for the "middleReel" variable. that is now also "cherry".

Once it does that, it goes to line 3, and does the same thing for the "rightReel" variable. that is now also "cherry".

It then prints everything on lines 4-8.

since it is now running the inner-most loop, it jumps back to line 3, sets rightReel to "lemon", and prints everything again.
it jumps back to line 3, sets rightReel to "kumquat", and prints everything again.
it jumps back to line 3, sets rightReel to "rutabega", and prints everything again.

then, the innermost loop is done, since it ran through everything for rightReel. we leave the innermost loop, and the varialbe 'rightReel' goes out of scope and disappears.

it jumps back to line 2, and sets middleReel to lemon, and goes to line 3.

at this point, we create a NEW rightReel and set it to "cherry", and do the printing
it jumps back to line 3, sets rightReel to "lemon", and prints everything again.
it jumps back to line 3, sets rightReel to "kumquat", and prints everything again.
it jumps back to line 3, sets rightReel to "rutabega", and prints everything again.

then, the innermost loop is done, since it ran through everything for rightReel. we leave the innermost loop, and the varialbe 'rightReel' goes out of scope and disappears.

it jumps back to line 2, and sets middleReel to kumquat, and goes to line 3.

at this point, we create a NEW rightReel and set it to "cherry", and do the printing
it jumps back to line 3, sets rightReel to "lemon", and prints everything again.
it jumps back to line 3, sets rightReel to "kumquat", and prints everything again.
it jumps back to line 3, sets rightReel to "rutabega", and prints everything again.


etc...
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Diederik Lucas wrote:I do try to rollplay a computer when reading code. Still. Its an art to unerstand it fully if you ask me;)


Nah. Just experience. I've got 37 years, and I'm by no means the "senior" here.

As Arthur C. Clarke said:
"Any sufficiently advanced technology" ... and in this case I'd add 'experience' ... "is indistinguishable from magic."

And oddly enough, a simple pencil and paper can be your greatest friend when you're trying to "play computer".

Good luck.

Winston
 
Dirk Lucas
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thinking about the whole loop explanations and all, I can actually see to loop(s) runn when I close my eyes. Very clear!

Thank you all for help me seeing the logic in this. Good stuff!
 
and POOF! You're gone! But look, this tiny ad is still here:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic