• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nested if statements

 
RJ Espinoza
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all.
I am a new java student. I'm doing the exercises at the end of chapter 2, Deitel - Java how to program 5e.
Exercise 2.18 calls for programming which wasn't covered in the chapter. Part of the exercise is to determine the high and low numbers of three numbers entered by the user.
I solved it like so:

This does work, but is it propper for Java to use nested if statements?
[ September 15, 2003: Message edited by: RJ Espinoza ]
 
John Smith
Ranch Hand
Posts: 2937
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This does work, but is it propper for Java to use nested if statements?
Yes, you can nest "if", "for", "while", "switch" and "do" to eny level of depth. For the purposes of readability and the easier mainanace, however, most people would try to avoid deeply nested code structures. Also, for the same reasons, many would use the "{}" pair even when it is not required.

for some reason I can't indent my code in this post?
Use the *code* UBB, not the *quote* UBB.
 
RJ Espinoza
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply and the code Vs. quote tip.
 
Donald R. Cossitt
buckaroo
Ranch Hand
Posts: 401
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try something like this:
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33720
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, notice that

is the same as

But the second one is easier to read.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Donald's algorithm is right on. I wind up using that kind of thing a lot. Let's solve both problems at once with it:
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic