Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write multiple statements in Ternary operator?

I am posting this question because I found nothing similar regarding this type of post. I am learning ternary operators. I want to perform action like shown below:

  bool Divisible = false;
  foreach (var Number in NumberList))
  {
    var  Number = 242;
    if ((Number %= 2) | (Number %= 6))
    {
       Divisible = true;
    }
    else
    {
       Divisible = false;
    }
  }

We can write this using the ternary operator like this:

var Divisible = (Number %= 2 | Number %= 6) ? false : true ;

But if in else block there are multiple statements then what to do?

  bool Divisible = false;
  foreach (var Number in NumberList))
  {
    var  Number = 242;
    if ((Number %= 2) | (Number %= 6))
    {
       Divisible = true;
    }
    else
    {
       Divisible = false;
       break;
    }
  }

How can we write ternary operator with multiple else statements? Please share your knowledge of ternary operators.

like image 929
Sanket Thakkar Avatar asked Aug 08 '14 10:08

Sanket Thakkar


People also ask

Can we use multiple statements in ternary operator?

Yes, we can, but with one proviso… There is no block demarcation so that action should be simple, else it would be better to abstract it away in a function and call the function from the ternary. Something we commonly see is console.

How do you perform multiple operations in a ternary operator?

You can use the comma operator to execute multiple expressions in place of a single expression: arr[i] % 2 === 0? (evenCount++, totalCount++) : (oddCount++, totalCount++); The result of the comma operator is the result of the last expression.

How do you write 3 conditions in a ternary operator?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.


1 Answers

But if in else block there are multiple statements then what to do?

If the statements are unrelated, then you don't use the conditional operator. Just use an if-else like you already have.

In your case, since your code needs to break if and only if Divisible is set to false, then you cannot use a conditional operator even if you wanted to hack it in, because a break is a statement, not an expression, and therefore cannot appear as part of a conditional operator.

In general, you only use the conditional operator when you want to decide between assigning one of two values based on a condition. For anything else, you should really be using a regular if-else construct. Don't try to shoehorn the conditional operator into just about any decision-making code because you'll more often than not find yourself running into problems such as this one.

like image 125
BoltClock Avatar answered Nov 15 '22 00:11

BoltClock