This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nesting

 
John Kirvan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does java allow unlimited nesting.

For example I had a do while loop. within the do i have an, if, than an else if, and within my else if i have a for loop and the yet another if inside the for. Will I be able to do that
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. Better yet, try it out and see what happens!
 
John Kirvan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Kirvan:
Does java allow unlimited nesting.

For example I had a do while loop. within the do i have an, if, than an else if, and within my else if i have a for loop and the yet another if inside the for. Will I be able to do that


So when you say sure your joking i guess
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think he is not joking, but he wants you to prove it to yourself.
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoops, sorry about that...I wasn't even being sarcastic, although I can be! I meant that yeah, Java can definitely handle nesting with loops. Try it out...how would you initialize a matrix of numbers? Say four columns across, and four rows down, and initialize them all to zero, or whatever you want...you'll have to do a bit of nesting...but you can definitely nest as much as you want, actually.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder about the "unlimited" part. I suspect that there is probably some limit. At the very least, this limit is based on the amount of memory in your machine that is available for the compiler. However, even if there is a hard limit, it is probably rather large. In this case, the nesting will work fine. If you are interested in trying to see if there is a limit, I would suggested making A LOT of nested loops and/or if statments. By "a lot", I would guess more than 255. (Typically I would advise to NOT write real code this way, but it can be an interesting exercise to see if there is an actual limit.) Also, you can check out the Java Language Specification to see what it says on this matter.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic