sai rama krishna wrote:
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
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.
Dave Tolls wrote:Even though the flow is different between the two bits of code, the end result is the same.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
If instead you have a bunch of if statements, it is possible that any or all will execute.
When you have if with a bunch of else-if...else statements, at MOST one will run.
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.
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?
Grade = D
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.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 have to understand, that you cannot state, that one is more useful to use than the other - these are different.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!)
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
|