英文:
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 << endl
<< "1. Another equation" << endl
<< "2. Exit Calculator" << endl;
int afterCalc;
cin >> 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
条件运算符要求其操作数是表达式。goto
和 continue
是语句而不是表达式,所以这个运算符不能与它们中的任何一个一起使用。
语句是程序的指令 - "要做什么";它们通常以分号 ;
或右大括号 }
结尾。
表达式是代码片段,其值应该被用在其他表达式或语句中。在表达式中使用流程控制指令是没有意义的。
更多信息请参阅cppreference。
作为一般准则,你应该使代码易读。这一点在确实必须使用 goto
和 continue
时尤为重要 - 这些是代码执行某些晦涩而重要的操作的罕见情况,你应该尽量使用最可读的方式来表达你的代码!也就是说,将其保留为 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 << "Do something useful here" << endl;
return nullptr;
}
int main() {
int afterCalc;
do {
cout << endl
<< "1. Another equation" << endl
<< "2. Exit Calculator" << endl;
cin >> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论