File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Nested if statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nested if statements" Watch "Nested if statements" New topic

Nested if statements

RJ Espinoza

Joined: Jan 10, 2003
Posts: 12
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

Joined: Oct 08, 2001
Posts: 2937
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

Joined: Jan 10, 2003
Posts: 12
Thanks for the reply and the code Vs. quote tip.
Donald R. Cossitt
Ranch Hand

Joined: Jan 31, 2003
Posts: 401
You could try something like this:

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33132

Also, notice that

is the same as

But the second one is easier to read.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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:

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
I agree. Here's the link:
subject: Nested if statements
jQuery in Action, 3rd edition