• 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 ..with else if ...with else if..with else vs if..if...if...else approach

 
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,



When do we use above if ..if..if syntax with else



when do we use above if ..with else if ...with else if..with else kind of syntax.( here it filters down more and more as we go down right?)
are there any use cases in which one is preferred over other
Please advise
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps, it is a good idea to format the code correctly? This ...

sai rama krishna wrote:



should probably be better formatted as ...



sai rama krishna wrote:
when do we use above if ..with else if ...with else if..with else kind of syntax.( here it filters down more and more as we go down right?)
are there any use cases in which one is preferred over other



There isn't really a preference of one over the other. These two programs have different execution flows -- you choose the one that is appropriate for your program.

Henry
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks like a candidate for a switch statement.
 
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

Henry Wong wrote:There isn't really a preference of one over the other. These two programs have different execution flows -- you choose the one that is appropriate for your program.


Henry is right. These two programs are not the same!

Try setting x = 10 and run both versions. You'll see you will get different results.

If you add { ... } to the else statements, it becomes more clear what the second version with the else actually does. You can see that this is different from the version without the else:

 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even though the flow is different between the two bits of code, the end result is the same.
However (to highlight the difference) if the checks were something like this:

compared to:

Then, with an 'x' of 30, the first one will produce:

Value of X is greater than 30
Value of X is greater than 20
Value of X is greater than 10

and the second will produce:

Value of X is greater than 30
 
Jesper de Jong
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

Dave Tolls wrote:Even though the flow is different between the two bits of code, the end result is the same.


No, it isn't.

Try x = 10 and you'll see different results.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code has:


I suppose I could have just changed 'x'...;)
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you have if with a bunch of else-if...else statements, at MOST one will run.

If instead you have a bunch of if statements, it is possible that any or all will execute.

Which you use depends on what you want to happen.
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


above gave below output

Value of X is greater than 20
Value of X is greater than 10


Which is like normal processing of java right where it comes one by one to the next line down like you mentioned below?

If instead you have a bunch of if statements, it is possible that any or all will execute.






above gave below output
Value of X is greater than 20 which proved below point

When you have if with a bunch of else-if...else statements, at MOST one will run.


My control A and Control Shift F to format the code in eclipse seems not indenting deep enough for some reason for else if as above. Please advise if i should use any custom formatting for that?

Which you use depends on what you want to happen.

There isn't really a preference of one over the other. These two programs have different execution flows -- you choose the one that is appropriate for your program


That looks like a candidate for a switch statement.



Any good resource or link which explain for what use case or scenario i should use which statement. Please advise.

To me looks like writing multiple if's like below useless in case of >




where as for == comparison it is fine to have either of multiple if's or if with else if approach.


 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


above else under if approach gave below output

Value of X is 30.


What are all different approaches for these control statements available?
I saw about 3,4 different approaches for this simple example( apart from if and switch approach i read earlier)
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sai rama krishna,

As guys mentioned above - it depends on your program requirements, and what program intended to do.
The most recent code example you posted, indeed is not a good approach to this problem (at least because of code readability), since you're digging inside and increasing nesting level each time. Actually even difficult to describe whats happening and could be misleading in explanation.

One of the right approaches to this problem (I assuming this problem as a classic example):

sai rama krishna wrote:My control A and Control Shift F to format the code in eclipse seems not indenting deep enough for some reason for else if as above. Please advise if i should use any custom formatting for that?


I'd recommend you do not use these formatting features at the beginning at all (while you still learning fundamentals), as it can lead to misunderstand the concept.
Actually, you even could use some simpler editors, as Notepad++ if you're using Windows, or TextWrangler if Mac OS X, or Vim, Emacs if Unix like OS. It would help you better understand and master Java language itself.

How the code should be correctly formatted and indented you could read about it here (<-- link). Document content is quite old, but still applied in practice.
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i love this link

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

I ran both if-then and if-then-else-then exampleas as below

out put is

Grade = C


output is
finally current speed is 10//i wonder why i have not got 9 as i applied break? please advise

my main question still revolves around if -then-if-then demo as below

output i got is

Grade = D




why i got Grade = D IfElseDemo gave Grade = D.

In this case IfThenIfThenDemo utput is wrong right.

Please advise.

If this is case what would be use case when we have to use IfThenIfThenDemo
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read your code carefully and you should be able to work out why you got C first time and D second time.

In the one about reducing speed, look closely at lines 3 and 8.
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



i see the point.
Above code produced below
Grade = C

Which means the if i have multiple if's then jvm executes every one one after other.
Intially it executed

if (testscore >= 60) {
grade = 'D';
}

Printed
grade = 'D';

then jvm executed below (//after if (testscore >= 80) { even though it did not enter this if block as boolean check is false)
if (testscore >= 70) {
grade = 'C';
}
As the above conition is true i got out put
Grade = C//which has overrided Grade = D looks like.
Please correct my understanding is wrong.
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even though the flow is different between the two bits of code, the end result is the same.
However (to highlight the difference) if the checks were something like this:



compared to:



Then, with an 'x' of 30, the first one will produce:

Value of X is greater than 30
Value of X is greater than 20
Value of X is greater than 10

and the second will produce:

Value of X is greater than 30





i do not find even one useful use case where and when we should use multiple if's (probably none existing!) as in the first case of this comment which gives multiple times similar output
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:Then, with an 'x' of 30, the first one will produce:

Value of X is greater than 30
Value of X is greater than 20
Value of X is greater than 10

and the second will produce:

Value of X is greater than 30

You are not right.

The first would produce:
Value of X is greater than 20
Value of X is greater than 10

Second:
Value of X is greater than 20

sai rama krishna wrote:i do not find even one useful use case where and when we should use multiple if's (probably none existing!)

You have to understand, that you cannot state, that one is more useful to use than the other - these are different.

In first case, multiple "if's" don't matter if first "if" condition is satisfied, it will check second and third. In second case - once one of these are satisfied, the ones below him wouldn't be checked.
 
I think I'll just lie down here for a second. And ponder this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic