0

I'm trying to use a ternary expression along with a continue or break construct inside a do-while loop in Dart but I get a compilation error.

do
{
  expr ? print('Conditionally print something') : continue;

}while(expr == true);

The above code fails at compile-time but if I use a pair of if-else decision structure, the code works. So, the question to the community is why the ternary expression is not working along with continue or break construct?

yaddly
  • 340
  • 2
  • 14

3 Answers3

1

The ternary operator takes 3 expressions in the form of (expr1) ? (expr2) : (expr3).

You can't execute statements in ternary operator, nor only in dart but in other languages also. Since break and continue are not expressions but statements they cant be used here

Aman Verma
  • 818
  • 5
  • 17
  • Thank you for your kind response, would you care to elaborate why this is so? – yaddly Apr 26 '21 at 17:46
  • 1
    The ternary conditional operator is an operator that combines multiple expressions into a larger expression. An expression must evaluate to something. A statement performs an action. Expressions can be used as statements, but the reverse is not true. – Aman Verma Apr 26 '21 at 17:52
  • I tried wrapping the continue statement in an anonymous function but still it failed as well, that also boggled my mind? – yaddly Apr 26 '21 at 18:06
  • that is because you can't use control structure statements like break , continue outside loops – Aman Verma Apr 26 '21 at 18:08
  • The statement was inside a loop; the else part of the ternary expression, but I understand now, thank you for your elaborate explanations – yaddly Apr 26 '21 at 18:12
  • 1
    what I meant was when you wrap continue in function , that function has no access to loop , no matter even if you keep that function in loop , for eg try keeping the function in for loop , can you access the counter variable without function arguments , no . This works similarly . – Aman Verma Apr 26 '21 at 18:20
1
  • No, you can't do it.
  • continue is a Dart Statement (read below), and print is a function

First

17.23 Conditional conditional

A conditional expression evaluates one of two expressions based on a boolean condition.

{conditionalExpression} ::= {ifNullExpression} (‘?’ {expressionWithoutCascade} ‘:’ {expressionWithoutCascade})? Evaluation of a conditional expression c of the form e1?e2 : e3 proceeds as

follows: First, e1 is evaluated to an object o1. It is a dynamic error if the runtime type of o1 is not bool. If r is true, then the value of c is the result of evaluating the expression e2. Otherwise the value of c is the result of evaluating the expression e3.

Second

As you can see the ternary operator requires expressions, but continue, in the same PDF of language specification is defined as an statement, a reserved word, as:

18 Statements statements

A statement is a fragment of Dart code that can be executed at run time. Statements, unlike expressions, do not evaluate to an object, but are instead executed for their effect on the program state and control flow

Third

in the case of print, it's taken as a function I guess, didn't find the specification. Perhaps it returns void. We can still ask ourselves, why can't we put continue in a function, even in a lambda like () => { continue; } or similar. The short answer is that as said in the specification for the continue statement, if it's not inside a while, etc. is gonna give a compile error. And if it's inside a function, it will prevent that function to reach the return statement, and again, the ternary will expect a return value.

We can still research a little more, many things are inside the specification.

zurcacielos
  • 1,495
  • 2
  • 9
  • 7
0

Because a ternary operator is returning a value. With the following syntax:

condition ? expresion1 : expression2

which means:

If condition is true, it return expression1. If it is not it return expression2.

So, you can't use statement like this:

print('Conditionally print something')

or

continue

An expression evaluates to a value. A statement does something.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • 1
    You can use `print ("Anything " )` in ternary operator – Aman Verma Apr 26 '21 at 17:58
  • Try running this `void main() { var a = 1; (a==1) ? print("abc"): print('xyz'); }` – Aman Verma Apr 26 '21 at 18:01
  • 2
    `print('Anything')` can be used with a ternary operator. `void main() { true ? print('hi') : print('bye');` is perfectly valid. `print('Anything')` is still an *expression*, but the *result* of that expression is of type `void`, and that means that the result cannot be *used*. – jamesdlin Apr 26 '21 at 18:03
  • void main() { var a = 1; (a==1) ? print("abc"): print('xyz'); } this would work without issues, I just wanted to understand why I can't alter the loop execution with continue statement using a ternary expression. The focus being on why, not how to use ternary expression. Thank you very much on your kind contributions – yaddly Apr 26 '21 at 18:23