如何写“如果 x 等于 5 或 4 或 78 或...”在C中
我有一个关于在 if 语句中使用逻辑运算符的快速问题。
目前我有一个 if 语句来检查 x 是否等于 5 或 4 或 78:
if ((x == 5) || (x == 4) || (x == 78)) {
blah
}
我想知道我是否可以将所有这些压缩为:
if (x == 5 || 4 || 78) {
blah
}
抱歉这么基本的问题,我刚刚开始学习 C。
I have a quick question about using logical operators in an if statement.
Currently I have an if statement that checks if x equals to 5 or 4 or 78:
if ((x == 5) || (x == 4) || (x == 78)) {
blah
}
And I was wondering if I could just condense all that to:
if (x == 5 || 4 || 78) {
blah
}
Sorry for such a basic question, I've just started learning C.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
没有捷径,但您需要修复相等运算符。
There is no shortcut, but you need to fix your equality operator.
首先,您在 if 中使用的是赋值而不是相等测试。第一种方法(用适当的相等替换)是进行测试的最佳方法,但如果您有很多可能的选择,可能会有更好的方法。第二种方法可能会编译,但它不会做你想要的,它总是返回 true,因为 4 和 78 都评估为 true,而你正在做的是评估 5 (将 5 分配给 x 的结果)或 4 或78 是真的。 switch 语句可能是一种可能的替代方案。
First, you're using assignments not equality tests in your ifs. The first method (with suitable substitutions for equality) is the best way to do the test, though if you have a lot of possible options, there might be better ways. The second way might compile, but it won't do what you want, it will always return true since both 4 and 78 evaluate to true and what you are doing is evaluating whether 5 (the result of assigning 5 to x) or 4 or 78 are true. A switch statement might be one possible alternative.
if 语句没有捷径,但我建议考虑:
There's no shortcut for the if statement, but I suggest considering:
不,你不能,相等的测试是
==
,而不是=
No you cannot and the test for equality is
==
, not=
@uncle brad 说得很对,但稍后您可能会了解称为
switch
语句的东西。它看起来很时髦,但经常在这些情况下使用(其中变量的多个可能值都具有相同的效果):尽管您只想在以下情况下使用
switch
语句:if
语句不太清晰——现在大多数编译器都足够聪明,可以以任何一种方式生成最佳的机器代码。@uncle brad is spot on, but later you'll probably learn about something called a
switch
statement. It looks funky but is often used in these sorts of situations (where several possible values of a variable all have the same effect):Though you'd only want to use a
switch
statement when the meaning of anif
statement is less clear--most compilers these days are smart enough to generate optimal machine code either way.我在登录时就已经回答了这个问题,但是您可以使用开关,并将其分解为一个函数,
然后您只需在每次需要根据已建立的情况检查 int 时调用该方法即可。
诚然,这个示例相当愚蠢,但是对于更多的案例选择以及重复评估的案例,您可以执行类似的操作来简化和重用一些代码。
It's been answered in the time it took me to log in, but you could use the switch, and break it out into a function
Then you would just call the method every time you needed to check the int against the established cases.
Admittedly, this example is rather silly, but for a bigger selection of cases, and ones that were evaluated repeatedly, you could do something like this to simplify and reuse some code.
不,抱歉,你不能;你必须写出所有的表达式。对于要比较的很长的数字列表,您可以将数字放入数组中,然后循环遍历列表;但在这看起来是个好主意之前,你必须有十几个左右的数字。
No, sorry, you can't; you have to write all the expressions out. For very long lists of numbers to compare to, you could put the numbers in an array, and loop over the list; but you'd have to have a dozen numbers or so before that started to look like a good idea.
不,您不能在 C 中执行此操作。您的第一个代码示例也不正确。 C 中的赋值 (
=
) 和等价 (==
) 之间存在重要区别。当您在表达式中编写x = 5
时,在与表达式的下一部分进行逻辑或运算之前,这实际上会编译并计算为 0 或 1(假或真)!您的第二个代码示例也是有效的 C,但它没有执行您想要的操作。您将该语句读作“(x 被赋值为 5)或 true 或 true”。这是因为 C 中的任何非零值在逻辑上都是正确的。因此,x 将包含值 5,并且计算结果为 true,从而使您的 if 条件为 true。表达式的其余部分并不重要,因为
||
运算符会短路。No, you cannot do this in C. Your first code sample is also incorrect. There is an important distinction between assignment (
=
) and equivalency (==
) in C. When you wrotex = 5
in your expression, this will actually compile and evaluate to either 0 or 1 (false or true) before being logically OR'ed with the next part of the expression!Your second code sample is also valid C, but it does not do what you want it to do. You read the statement as "(x is assigned to 5) or true or true". This is because any non-zero value in C is logically true. Thus, x will contain the value 5, and evaluate to true, making your if condition true. The rest of the expression does not matter since the
||
operator short-circuits.另一种想法是:虽然没有“捷径”,但如果您有很多数字,那么代码长度和输入理智可能会更容易将它们全部放入数组中并在循环中检查数组。如果您必须多次比较许多数字,请将它们排序在数组中并使用二分查找。
但对于 3 个数字,您必须以“长”的方式进行。
One alternate thought: While there's no "shortcut", if you have a lot of numbers it may be easier for code length and typing sanity to put them all into an array and check against the array in a loop. If you have to compare against many numbers more than once, sort them in an array and use binary searching.
For 3 numbers, though, you have to do it the "long" way.
如果列表很长,您可以执行 for 循环,但是这并不能真正处理逻辑运算符:-...
呃...还没有做过 c...曾经...您应该采用 C++.. 。
You can do the for loop if it's a long list, how ever that doesn't really handle the logical operators :-...
er... haven't done c... ever... you should take C++...
语法是:
回答你的问题:
The syntax is:
Answer to your question: