Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Nested if statements  RSS feed

 
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
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try something like this:
 
author & internet detective
Sheriff
Posts: 38561
654
Eclipse IDE Java VI Editor
  • 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)
Ranch Hand
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:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!