# about conditonal if else and switch case

• 09-22-2011, 09:09 AM
niksipandit
about conditonal if else and switch case
hello sir,
Is there any way to replace if-else and switch statement to any other logic.my question is that if we change if-else to some another thing ..the how it will easier to me ...
• 09-22-2011, 09:26 AM
JosAH
Re: about conditonal if else and switch case
A sequence of if-else if-else if ... else statements can be simulated by a Map<Condition, Runnable> where the Condition object represents the conditon in an if (...) clause; a switch statement is more difficult to handle with a Map because of the 'fall through' behaviour.

kind regards,

Jos
• 09-22-2011, 09:39 AM
Re: about conditonal if else and switch case
It depends on the conditions, if they are numbers you can manipulate them using logical operators, for example:

Quote:

double x = ...
if(x >= 0) System.out.println("x is greater than or equal zero!");
else System.out.println("x is negative number!");
you can do this without if statement:

Quote:

double x = ...
String[] arr = {"x is greater than or equal zero!", "x is negative number!"};
System.out.println(arr[(int) Math.ceil(x) >>> 31]);
• 09-22-2011, 10:53 AM
niksipandit
Re: about conditonal if else and switch case
hey jos'
can u pls show it to by some example.

thanks alot
• 09-22-2011, 11:09 AM
Anoop
Re: about conditonal if else and switch case
You can check java api by typing java api 1.6 in google...

that has complete detail about java. and just go for java.util package there. You'll c Map and other alternatives of if-else.

Thanx
Anoop
• 09-22-2011, 11:16 AM
niksipandit
Re: about conditonal if else and switch case
thanks anoop,
but i need also to know that how map is usefull to me over on if-else. for example if i have several if-else in my code and i replace them all by map..so,how they are useful to me.
• 09-22-2011, 11:53 AM
niksipandit
Re: about conditonal if else and switch case
dear jos,

what do u meant by runnable there..can u show it by an example ..its very important for me ...please give answer as soon as possible

thanks