How to turn if, else if logic into a ternary operator?

后端 未结 6 1909
执念已碎
执念已碎 2020-12-28 20:12

I was just wondering if this was possible because i started using ternary operators to reduce lines of code and i am loving it.

if (x==y)
{
    z += x;
} els         


        
6条回答
  •  攒了一身酷
    2020-12-28 20:45

    Four lines of code, and the most readable, IMO. No need for a ternary operator here:

    if (x == y || x == z)
        z += y;
    else 
       z++;    
    

    If I had to write it using ternary, I would do:

    z += (x == y || x == z) ? y : 1;
    

提交回复
热议问题