This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Nested IF's Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nested IF Watch "Nested IF New topic

Nested IF's

jaime craven

Joined: Feb 17, 2004
Posts: 3
Hello everybody..
I've written this code below as I'm teaching myself java bit by bit but I'm having a problem with IF statements, I know below I havent used nested IF's and that was my goal so if somebody could give me an example of how to write my code in a nested IF format I'd be overjoyed.

[ February 17, 2004: Message edited by: Jim Yingst ]
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
nested ifs just means you have an if within another if, ie:

Armed with this knowledge, you should be able to implement nested ifs on your own.
jaime craven

Joined: Feb 17, 2004
Posts: 3
I know its an IF within an IF but the thing is I got in my code...

and then the next statement being...

and I really just can understand or see how to make them nested maybe I am blind I dunno but I just cant figure it out especially with the counter bit in there it confuses me alot..
Stuart Chambers

Joined: Feb 01, 2004
Posts: 7
Hi Jaime,
I would modify your If statements and For loops in the following way (the code after the "\\close while loop" statement - I have removed the comments as you can add them back in as you require):

The If-Else structure ensures that once the code has been performed for the relevant selection, the following For loops are not performed as they are not necessary.
I hope this helps...
[ February 17, 2004: Message edited by: Stuart Chambers ]
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
I edited a couple really long lines of code that were making the rest of the page hard to read because they forced the page to be really wide. Also, I recommend you should not use tabs for indentation. Different systems interpret them differently - most web browsers treat them as equivalent to 8 spaces. Which means it's very easy to force the page much wiser than it needs to be. Four spaces per indent is probably the max you should use; many use two. (A few use three, but they're just weirdos.)

"I'm not back." - Bill Harding, Twister
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

I would recommend using a switch:
switch (option)
case 1: for (/*...) ... */ break;
case 2: for (/*...) ... */ break;
case /*... */
Of course you should use tabs for indentation, not spaces. Since most editors may show an tab as if it's three or four spaces, though the correct value is of course 8.
If you get code from different coders, which prefer different tab-widths, it's irrelevant, when all use tabs to save the code.
If they use different settings and store them as spaces, you cannot revert them properly.
And tabs jump to fix positions, not to distances.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I use Eclipse and hit ctrl-shift-F on any code that I hope to read. I love how everybody codes in exactly my formatting style!

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Have you checked out Aspose?
subject: Nested IF's
It's not a secret anymore!