• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

if and else if

 
Ranch Hand
Posts: 50
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ken Austin
Ranch Hand
Posts: 50
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jelle, that is exactly the type of explanation I was looking for. Thank you very much!
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...).
 
Ken Austin
Ranch Hand
Posts: 50
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fred, that is helpful, as well. Thank you. That does make it a lot clearer.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic