Results 1 to 5 of 5
  1. #1
    Arez is offline Member
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default Do this without conditionals...

    say I have this:

    Java Code:
    boolean state=false;
    int x=0;
    int y=0;
    
    if (x > y)
      if (y != 0)
       state=true;
      else
       if (x == 0)
        state=true;
    
    else
     state=false;
    how would I write this without using conditional statements?

    thanks..

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

  3. #3
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    Java Code:
    boolean state=false;
    		state = (x>y) ? ((y!=0)? true:((x==0)?true:false)): false;

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Ternary operator can include many number of conditional statements, but it's not good practice to use more.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    without using conditional statements
    How can you make decisions in your code without testing conditions? How is the ternary operator not testing a condition?
    Sounds like lawyer talk to say its not using conditions.

Posting Permissions

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