This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Nesting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nesting" Watch "Nesting" New topic
Author

Nesting

John Kirvan
Greenhorn

Joined: Feb 01, 2005
Posts: 12
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

Joined: Jul 15, 2003
Posts: 618
Sure. Better yet, try it out and see what happens!
John Kirvan
Greenhorn

Joined: Feb 01, 2005
Posts: 12
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

Joined: Jul 22, 2000
Posts: 9044
    
  10
I think he is not joking, but he wants you to prove it to yourself.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Nesting
 
Similar Threads
Nesting
Help for Nesting
Metthod within method need sloving
Reducing nesting: A good practice?
A flow control issue