wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes if else statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "if else statement" Watch "if else statement" New topic
Author

if else statement

natalie chua
Greenhorn

Joined: Apr 06, 2008
Posts: 3
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

Joined: Jan 17, 2006
Posts: 1296
Lets try with some formatting and with all optional braces explicitly included:


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
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

Joined: May 18, 2007
Posts: 341

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

Joined: Apr 06, 2008
Posts: 3
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

Joined: Oct 13, 2005
Posts: 37976
    
  22
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

Joined: Jan 01, 2007
Posts: 333
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

Joined: May 18, 2007
Posts: 341

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

Joined: Apr 06, 2008
Posts: 3
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

Joined: May 18, 2007
Posts: 341

Well that was just some juggling of words. Hope your doubts are all claer now.
 
wood burning stoves
 
subject: if else statement
 
Similar Threads
doubt on this question(from a Mock Test)
Apply function names as parameters in other functions
Garbage Collection - Practice
x = b ? y : z ;
int Addition/Subtraction Overflow Detection