Ternary operator vs VSCode “expected expression error”

huangapple go评论88阅读模式
英文:

Ternary operator vs VSCode "expected expression error"

问题

I tried to change a simple if/else statement into a ternary operator, but after typing it as:

cout << endl
     << "1. Another equation" << endl
     << "2. Exit Calculator" << endl;
int afterCalc;
cin >> afterCalc;

(afterCalc == 1) ? goto calcStart : continue;

I get an "expected expression" notice, and the code doesn't debug/compile. Is there something wrong with the code, or is it a Visual Studio Code (VSC) bug?

英文:

I tried to change simple if/else statement into ternary but after typing it as:

cout &lt;&lt; endl
         &lt;&lt; &quot;1. Another equation&quot; &lt;&lt; endl
         &lt;&lt; &quot;2. Exit Calculator&quot; &lt;&lt; endl;
    int afterCalc;
    cin &gt;&gt; afterCalc;

    (afterCalc == 1)? goto calcStart : continue;

I get "expected expression" notice and code doesnt debug/compile. Is there sth wrong with the code or it's VSC bug?

答案1

得分: 2

条件运算符要求其操作数是表达式gotocontinue 是语句而不是表达式,所以这个运算符不能与它们中的任何一个一起使用。

语句是程序的指令 - "要做什么";它们通常以分号 ; 或右大括号 } 结尾。

表达式是代码片段,其值应该被用在其他表达式或语句中。在表达式中使用流程控制指令是没有意义的。

更多信息请参阅cppreference


作为一般准则,你应该使代码易读。这一点在确实必须使用 gotocontinue 时尤为重要 - 这些是代码执行某些晦涩而重要的操作的罕见情况,你应该尽量使用最可读的方式来表达你的代码!也就是说,将其保留为 if 语句,也许添加注释来解释你的代码做了什么。

或者,重写你的代码,以便不使用 goto

英文:

The conditional operator requires expressions as its operands. goto and continue are statements and not expressions, so the operator can't work with any of them.

Statements are instructions for the program - "what to do"; they usually end with a semicolon ; or a closing brace }.

Expressions are fragments of code whose value should be used — in other expressions or in statements. It doesn't make sense to use flow control instructions in an expression.

See cppreference for more info.


You should make code readable, as a general guideline. This is even more important if you really must use goto and continue — these rare cases are when the code does some obscure and important stuff, and you should try to use the most readable way to express your code! That is, leave it as if-statement, and maybe add comments to explain what your code does.

Alternatively, rewrite your code so that it doesn't use goto.

答案2

得分: 1

以下是您要翻译的内容:

三元运算符不用于控制流。三元运算符虽然能够在条件中包含代码,但它是一种产生左值的赋值操作。

我不确定为什么您想要在这种情况下更改if/else,但一种方法(也许不是最干净的方法)是:

void *something_else() {
    cout << "在这里执行有用的操作" << endl;
    return nullptr;
}

int main() {
    int afterCalc;
    do {
        cout << endl
            << "1. 另一个方程" << endl
            << "2. 退出计算器" << endl;
        cin >> afterCalc;
        void *foo = (afterCalc == 1) ? something_else() : nullptr;
    } while(afterCalc == 1);
}

我不知道您的用例,我不会这样做,但它是有效的。两个左值的数据类型必须相同,因此需要返回一个void指针。

英文:

The ternary operator is not for control flow. The ternary operator, while being able to accommodate code within the conditions, is an assignment that produces an lvalue.

I'm not sure why you would want to change an if/else in this case, but one way to do it (perhaps not the cleanest) is:

void *something_else() {
	cout &lt;&lt; &quot;Do something useful here&quot; &lt;&lt; endl;
	return nullptr;
}

int main() {
	int afterCalc;
	do {
		cout &lt;&lt; endl
			&lt;&lt; &quot;1. Another equation&quot; &lt;&lt; endl
			&lt;&lt; &quot;2. Exit Calculator&quot; &lt;&lt; endl;
		cin &gt;&gt; afterCalc;
		void *foo = (afterCalc == 1) ? something_else() : nullptr;
	} while(afterCalc == 1);
}

I don't know your use case and I would not do this, but it works. BOTH of the LVALUE's must be the same data type, hence the need to return a void pointer.

huangapple
  • 本文由 发表于 2023年5月25日 19:12:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定