• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nested IF's

 
jaime craven
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
thanks

[ February 17, 2004: Message edited by: Jim Yingst ]
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would recommend using a switch:
switch (option)
{
case 1: for (/*...) ... */ break;
case 2: for (/*...) ... */ break;
case /*... */
default:
}
JY:
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic