Win a copy of Hello World this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Nested if statements

 
Greenhorn
Posts: 12
  • 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 ]
 
Ranch Hand
Posts: 2937
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply and the code Vs. quote tip.
 
buckaroo
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try something like this:
 
author & internet detective
Posts: 39837
797
Eclipse IDE VI Editor Java
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • 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:
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!