Being Java programmer.
Ganish Patil wrote:Then there is no break in that case...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Ganish Patil wrote:At this point 9 and 5 are int then implicitly widening conversion took place so they become float like x = 9.00f and y = 5.00f
float x = 9;
float y = 5;
( x / y) = 9.00f / 5.00f and answer is 1.8f i.e. float then we are storing it in z which is an int variable. Explicit Narrowing conversion took place
so z = 1 now.
case 1 gets executed x = x + 2 means 9 + 2, x becomes 11. Then there is no break in that case so case 2 also gets executed x = x + 3 means 11 + 3 x becomes 14 then
again there is not break even after case 2, so default also gets executed x = x + 1 means 14 + 1 so x becomes 15
Answer is 15.
yes absolutely,there might be some reasons that switch hasn't been designed with default break. There could be a situation where if a case 1 is executed then case 2 must also get executed along with case 1. But if case 2 is executed then case 1 is not mandatory to get executed, in such kind of situation switch without default break is useful. For an exampleHas anyone else ever thought that switch would have been much better off designed to default to break, but allow a continue statement if you want to continue to the next case?
Being Java programmer.
yes correct, It also works with enumerated types and you can also have switch with String statements from JDK 7 and later.can only be used on byte, short, int, String but not on float, double and long right?
Being Java programmer.
your most welcome!Excellent, thank you for the explanation.
Being Java programmer.
Winston Gutkowski wrote:Has anyone else ever thought that switch would have been much better off designed to default to break, but allow a continue statement if you want to continue to the next case?
"Il y a peu de choses qui me soient impossibles..."
Ganish Patil wrote:...there might be some reasons that switch hasn't been designed with default break.
Ganish Patil wrote:there might be some reasons that switch hasn't been designed with default break.
Ole Kristian Sandum wrote:In GoLang, if I remember correctly, a case in a switch defaults to break unless the next case statement directly follows the current (i.e there is no logic in between).
If you want to fall through you use continue at the end of the case logic.
Thoughts? Do you think this is better than how it is done in Java and C?
Ole Kristian Sandum wrote:Thoughts? Do you think this is better than how it is done in Java and C?
Jesper de Jong wrote:Probably because it's a feature that already existed in C and C++ in this way - Java inherited a lot of its features from those languages. When Java was invented in the 1990's, one of the goals was to create a language that would look familiar to C++ programmers.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Not looking good. I think this might be the end. Wait! Is that a tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|