This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi people, I want to know if there is a way to do something like this with a switch. If ( i>=10 && i<=20) then ... else if (i>=21 && i<=30) then ... else if (i>=31 && i<=40) then ... else ... With a switch it should be something like this switch (i) case ( i>=10 && i<=20):... case ( i>=21 && i<=30):... case ( i>=31 && i<=40):... It doesn't compile since the expression returns a boolean and we need an int (or something "smaller"). In ADA we may do case i is when 10..20=> ... when 11..30=> ... when 31..40=> ... when ... Is there any equivalent in Java or the only way to achieve it is to use the condition if??? And if there is no way, do you have an idea why are we forced to use an int with a switch? I think that a boolean would have been very helpfull, no? [ June 18, 2002: Message edited by: Younes Essouabni ]
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
hi all, in java switch takes a value that may be byte,short,char and int as argument and up to my knowledge at compile time the compiler construct a jump table on the basis of case labels and this make the excecuting of switch statement fast at runtime. and there are situations where use of switch is far effective than if structure.
as per your question you use switch rather then if structure using following code. we write program using
the result od using if structure and switch are same. [ edited to format code using the [code] and [/code] UBB tags -ds ] [ June 20, 2002: Message edited by: Dirk Schreckmann ]
Joined: Jan 13, 2002
I'm not sure that I understand what you're trying to explain me.
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Joined: Mar 17, 2002
hi,Younes Essouabni it is explanation part of your question that "Is there any equivalent in Java or the only way to achieve it is to use the condition if???" the second part of your question "And if there is no way, do you have an idea why are we forced to use an int with a switch? I think that a boolean would have been very helpfull, no? ". the reason of that i give last time that at compile time compiler create a jump table on basis of case label that is not possible if the boolean expresion is used as a case label.
Joined: Jan 13, 2002
Ok, I understand what you mean, but doing it this way I think it's not really a good solution, since the value I put were just for example. I could have put 10,32,48,23,54,... and the code won't work anymore (unless you find some hard algorithme ) Anyway thx awais and thx Marilyn