It's not a secret anymore!*
The moose likes Beginning Java and the fly likes if and else if Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "if and else if" Watch "if and else if" New topic
Author

if and else if

Ken Austin
Ranch Hand

Joined: Aug 20, 2012
Posts: 39

Hello, again...

I've realized that I really don't understand the difference between if, else and else-if. As I am working through the exercises in my textbook, I seem to apply them more or less interchangeably, and have been lucky so far that no logic error has popped up.

For instance...


The else-if parts work, but I can't explain to you why.

Quick site searches here and at Stack Overflow didn't turn up a clear and thorough explanation in one place. Can someone point me to a good thread or web page that has some clear examples of when and how to use these two similar but different constructions?

Thanks,
Ken
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

What exactly do you want to know? These keywords in Java mean more or less exactly what they mean in plain English. You could replace the word "else" by "otherwise", I don't know if that helps in understanding it:

Line 3: If the character ch is a letter and inWord is not true
Line 7: Otherwise, if the character ch is a whitespace character
Line 10: Otherwise, if the character ch is a newline


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

Don't think code for now think English. if I said to you "if you have the keys can you throw them over here, else can you help me find them"

You would know what I meant. Well now write that in java

Now for the extension


if you have the keys can you throw them over here, else if you have seen them where are they, else can you help me find them".

Hope this helps
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

A simple if-condition, the if body block may or may not execute:


An if-else construct, it's guarenteed that one and only one of the body blocks will execute, which one depens on the evaluation of the conditon:


An if-elseif construct, only one of the body blocks may execute, or none may execute depending on the evaluation of the conditons:


An if-elseif-else construct, it's guarenteed that one and only one of the body blocks will execute, which one depends on the evaluation of the conditons:


Then there's also the possibility of multiple if-conditions without else, one of the blocks may execute, they both may execute, or neither may execute, it depens on the evaluation of the conditons:

Etc. etc.

Does that make sense?

Edit: Crud, too slow...


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Ken Austin
Ranch Hand

Joined: Aug 20, 2012
Posts: 39

Jelle, that is exactly the type of explanation I was looking for. Thank you very much!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

Imagine you are a butler for someone. You are going to show up every day monday through friday to work for them, week after week. They give you a piece of paper that says:


That should be pretty clear.

Note that this COULD be written as a bunch of individual if statements that are independent of each other:


There is a subtle difference between the two ways of writing it. In the first way, ONLY ONE thing will be done. As soon as the first condition that is true is encountered, that block of code runs, and then execution jumps to after the 'else' block. In the second way, each and every case is tested, and it is possible more than one could run. In my second example above, that is unlikely to happen (Unless you start cleaning the office at 11:59 p.m. and then check at 12:01 a.m. to see if it is Tuesday...).


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ken Austin
Ranch Hand

Joined: Aug 20, 2012
Posts: 39

Fred, that is helpful, as well. Thank you. That does make it a lot clearer.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
The subtle difference Fred mentioned doesn’t show up for days of the week, because they are mutually exclusive. A common example is marks and grades:If you try:… without the elses, only Ds and fails will be recorded properly. If you get 80, then you get an A+, an A, a B, a C and a D all together. Or maybe each if will override the previous one and you will end up with a D for your 80. You could have got that for 40. Hardly seems worth the effort!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7795
    
  21

Ken Austin wrote:Fred, that is helpful, as well. Thank you. That does make it a lot clearer.

And just to add to what Fred and Campbell have said: this is one area where English and Java (or indeed, any computer language) differ.

In English, we will quite often leave out the 'else's because it's fairly obvious to anyone who's looking at it in written form; but when writing a logical expression, you MUST be explicit about everything.

HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: if and else if