Results 1 to 7 of 7
  1. #1
    niksipandit is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default 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 ...

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,523
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    FOUAD-SPIDER is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default 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:

    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:

    double x = ...
    String[] arr = {"x is greater than or equal zero!", "x is negative number!"};
    System.out.println(arr[(int) Math.ceil(x) >>> 31]);
    Last edited by FOUAD-SPIDER; 09-22-2011 at 09:49 AM.

  4. #4
    niksipandit is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Re: about conditonal if else and switch case

    hey jos'
    can u pls show it to by some example.


    thanks alot

  5. #5
    Anoop is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default 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

  6. #6
    niksipandit is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default 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.

  7. #7
    niksipandit is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default 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

Similar Threads

  1. java switch case
    By aconti in forum New To Java
    Replies: 16
    Last Post: 08-09-2011, 07:05 AM
  2. Converting if to Switch Case.
    By king2be98 in forum New To Java
    Replies: 5
    Last Post: 02-20-2011, 03:46 PM
  3. if else changes to switch-case?
    By noobinoo in forum New To Java
    Replies: 1
    Last Post: 04-23-2010, 05:56 PM
  4. Replies: 2
    Last Post: 02-23-2009, 12:34 AM
  5. Switch Case and Key Events
    By AndrewM16921 in forum New To Java
    Replies: 4
    Last Post: 01-26-2009, 11:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •