Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

if else statement

 
natalie chua
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell me if last else should associate with which if?
Personally think the last else should associate with the "if (y > 100)"
becoz the the rule says "else should associate with the nearest if in the same block and that if has not already been associated with an else"

However, some book says the correct answer should be if (x < 10).
So im confused now. Can someone tell me am i right or wrong?

if (x < 10)
if (y > 100) {
if (!done) x = z;
elsey = z;

}
else System.out.println("error");
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets try with some formatting and with all optional braces explicitly included:
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Garrett has done, you should always include the braces in all flow controll statements, espically when you only have one statement, it makes your code 100% more readable, and more maintable.

Have you tried to run the code? Or worked though the logic in your head?
Which if you do you think the last else belongs too?
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
natalie chua,
Welcome to JavaRanch.

A well formatted and aligned code is a feast to the eyes. Many problems like your current question become obvious seeing a well formatted code.

Maybe you'll like to use an IDE for writing code. Eclipse is a good one and is free.

Let us know if you were able to figure out the if-else association.
 
natalie chua
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to Garrett, he has given me a clear scope of the picture and taught me to "spread out" and embracing the code block with complete sets of braces.


Hi Gavin, Anubhav
I have tried to add some more else statements and run the code and the final result is the last else belongs to the "if (y > 100)". The result of running the test program coincides with my logic and understanding of the if-else statement.
However im not too confident of the result as the answer from that text book seems rather ambiguous. So i need some lights from the gurus..hehe
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Natalie Chua.

I agree that Eclipse is a very good tool for writing Java, but I think beginners are better using a decent text editor (look through the last few days' postings; I like Notepad++ on Windows and gedit on Linux), because of the steep "learning curve" for an IDE.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I agree with Campbell, for beginners using a text editor and learning to complie and run teh code by hand, is so much more helpful then just letting the IDE do it for you.

It also has an added advantage that you learn the common packages a lot easier, as you have to keep reaching for your prefered reference (mine was java in a nutshell, but the java doc for the packages is online), so you kinda learn which packages you have to import.
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by natalie chua:
Hi Gavin, Anubhav
I have tried to add some more else statements and run the code and the final result is the last else belongs to the "if (y > 100)". The result of running the test program coincides with my logic and understanding of the if-else statement.
However im not too confident of the result as the answer from that text book seems rather ambiguous. So i need some lights from the gurus..hehe[


Yes, indeed you got it correct. The else belongs to if (y > 100).
You can also check that by a simple test by altering value of x. If that else was for "x<10" check then if x is more than 10 it should print "error".

Well, kindly quote the refernce so we could verify if your question's answer is under errata section ...

As per the use of IDE. Lets stick to notepad or similar as fellow ranchers say.
 
natalie chua
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anubhav,
Thanks for your reply, now i can be quite sure i got the correct answer. Im using notepad to write sample codes too, though occasionally using ultra-edit editor in another pc.

The book im referring to is "Java 2 A beginner's Guide Second Edition", answer to question 3 under Mastery Checks for Moduke 3.

Quoted from the answer "The last <else> associates with the outer <if>, which is the nearest <if> at the same level as the <else> "

I think it would be clearer to say "The last <else> at the bottom associates with the second <if> from the top, which is the nearest <if> at the same level as the <else> "
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that was just some juggling of words. Hope your doubts are all claer now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic