39

how to use ternary if else with two or more condition using "OR" and "AND" like

    if(foo == 1 || foo == 2)
     {
      do something
      }
     {
      else do something
      } 

i want to use it like

  foo == 1 || foo == 2 ? doSomething : doSomething
4
  • 1
    Use parentheses to group expressions. Commented Feb 7, 2019 at 6:10
  • You can use parantheses to use logical "OR" and "AND". Commented Feb 7, 2019 at 7:05
  • 2
    What is the problem you are seeing? The code foo == 1 || foo == 2 ? doSomething : doSomething is valid, even without extra parentheses. Commented Feb 7, 2019 at 9:37
  • 1
    I would argue a slightly different point - why ternary over if/else? - Don't obfuscate the readability of your code unnecessarily. Commented Jun 1, 2020 at 23:21

10 Answers 10

87

If you're referring to else if statements in dart, then this ternary operator:

(foo==1)? something1():(foo==2)? something2(): something3();

is equivalent to this:

if(foo == 1){
    something1();
}
elseif(foo == 2){
    something2();
}
else something3();
Sign up to request clarification or add additional context in comments.

2 Comments

Nice. so ? and : just alternate always
The ? marks the end of the condition and the : simply means else
16

For three conditions use:

value: (i == 1) ? 1 : (i == 2) ? 2 : 0

Comments

5

Try below

(2 > 3)?print("It is more than 3"):print("It is less than 3");
////Prints It is less than 3 to the console

Comments

4

Try this:

foo == 1 ? doSomething1 : (foo == 2 ? doSomething1 : doSomething2)

If you have to include multiple conditions then you should use parantheses

Comments

3

it is easy,

if(foo == 1 || foo == 2)
 {
  do something
  }
 {
  else do something
  } 

it can be written thus for OR statement

foo==1 || foo==2 ? do something : else do something

it can be written thus for AND statement

foo==1 && foo==2 ? do something : else do something

both will work perfectly

Comments

3

EDITED

The original answer has run a little bit of from the question asked. Below is my edited answer.

To use ternary operator

(foo == 1 || foo == 2) ? doSomething() : doSomethingElse();

For my cleaner approach

{1, 2}.contains(foo) ? doSomething() : doSomethingElse();

ORIGINAL

The cleaner way for me is

if ({1, 2}.contains(foo)) {
  //do something
} else {
  //do something else
}

1 Comment

Thank you! This is the cleanest form for my case and reduced around 80 lines of code :D
3

For AND try this, // here both or multiple conditions needs to satisfy

if (primaryImageUploaded == true && signatureImageUploaded == true) {
    // status bool condition in true 
    } else {
     // if false 
    }

For OR try this, // here need ONLY any one condition to satisfy

if (primaryImageUploaded == true || signatureImageUploaded == true) {
    // status bool condition in true 
    } else {
     // if false 
    }

Another Dart Syntax

if (100 > 50) {
  print("100 is greater than 50");
 }

Comments

2

Here is an example of the same

Text((managerStatus == "pending")
    ? "Requested"
        : (adminStatus == "confirm")
    ? "Amount credited"
        : "Admin Pending")

Comments

2

Simple Multiple check in one condition

if(in_array($foo, [1,2,'other'])) {
    //do something
}
else {
    //else do something
}

Comments

-3
void main(){
  var a,b,c,d;  
  a = 7;  
  b = 9;  
  c = 11;  
  d = 15;  
  print((a>b)?((a>c)?((a>d)?a:d):c):(b>c)?((b>d)?b:d):(c>d)?c:d);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.