如何写“如果 x 等于 5 或​​ 4 或 78 或...”在C中

发布于 2024-10-21 21:58:19 字数 267 浏览 1 评论 0原文

我有一个关于在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

黯然#的苍凉 2024-10-28 21:58:20

没有捷径,但您需要修复相等运算符。

if ((x == 5) || (x == 4) || (x == 78)) {

There is no shortcut, but you need to fix your equality operator.

if ((x == 5) || (x == 4) || (x == 78)) {
梦醒灬来后我 2024-10-28 21:58:20

首先,您在 if 中使用的是赋值而不是相等测试。第一种方法(用适当的相等替换)是进行测试的最佳方法,但如果您有很多可能的选择,可能会有更好的方法。第二种方法可能会编译,但它不会做你想要的,它总是返回 true,因为 4 和 78 都评估为 true,而你正在做的是评估 5 (将 5 分配给 x 的结果)或 4 或78 是真的。 switch 语句可能是一种可能的替代方案。

switch (x) {
    case 4:
    case 5:
    case 78:
       blah...
       break;
    default:
}

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.

switch (x) {
    case 4:
    case 5:
    case 78:
       blah...
       break;
    default:
}
荒芜了季节 2024-10-28 21:58:20

if 语句没有捷径,但我建议考虑:

switch (x)
{
    case 4: 
    case 5:
    case 78:
        /* do stuff */
        break;

    default:
        /* not any of the above... do something different */
}

There's no shortcut for the if statement, but I suggest considering:

switch (x)
{
    case 4: 
    case 5:
    case 78:
        /* do stuff */
        break;

    default:
        /* not any of the above... do something different */
}
我纯我任性 2024-10-28 21:58:20

不,你不能,相等的测试是 ==,而不是 =

No you cannot and the test for equality is ==, not =

我的奇迹 2024-10-28 21:58:20

@uncle brad 说得很对,但稍后您可能会了解称为 switch 语句的东西。它看起来很时髦,但经常在这些情况下使用(其中变量的多个可能值都具有相同的效果):

switch (x) {
case 4:
case 5:
case 78:
    // ...
    break;
}

尽管您只想在以下情况下使用 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):

switch (x) {
case 4:
case 5:
case 78:
    // ...
    break;
}

Though you'd only want to use a switch statement when the meaning of an if statement is less clear--most compilers these days are smart enough to generate optimal machine code either way.

鸩远一方 2024-10-28 21:58:20

我在登录时就已经回答了这个问题,但是您可以使用开关,并将其分解为一个函数,

int isValid(int toCheck) {
   switch(toCheck) {
      case 4:
      case 5:
      case 78: 
         return 1;
      default:
         return 0;
   }
}

然后您只需在每次需要根据已建立的情况检查 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

int isValid(int toCheck) {
   switch(toCheck) {
      case 4:
      case 5:
      case 78: 
         return 1;
      default:
         return 0;
   }
}

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.

浪荡不羁 2024-10-28 21:58:20

不,抱歉,你不能;你必须写出所有的表达式。对于要比较的很长的数字列表,您可以将数字放入数组中,然后循环遍历列表;但在这看起来是个好主意之前,你必须有十几个左右的数字。

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.

维持三分热 2024-10-28 21:58:20

不,您不能在 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 wrote x = 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.

弄潮 2024-10-28 21:58:20

另一种想法是:虽然没有“捷径”,但如果您有很多数字,那么代码长度和输入理智可能会更容易将它们全部放入数组中并在循环中检查数组。如果您必须多次比较许多数字,请将它们排序在数组中并使用二分查找。

但对于 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.

就是爱搞怪 2024-10-28 21:58:20

如果列表很长,您可以执行 for 循环,但是这并不能真正处理逻辑运算符:-...

#include <stdio.h>

int forbiddenList[13] = {5, 4, 78, 34, 23, 56, 4, 7, 6, 4, 33, 2333, 0};
int length = 13;

int main() {

  int mysteryNum;

  printf("type a number: ");
  scanf("%d",&mysteryNum);

  int i;
  for (i = 0; i <= length; i ++)
    {

      int target = forbiddenList[i];

      if (mysteryNum == target)
        {
          printf("You have chosen of the forbidden list!\n");
          printf("YOU SHALL NOT PASS!!\n");
        }
    }
  return 0;
}

呃...还没有做过 c...曾经...您应该采用 C++.. 。

You can do the for loop if it's a long list, how ever that doesn't really handle the logical operators :-...

#include <stdio.h>

int forbiddenList[13] = {5, 4, 78, 34, 23, 56, 4, 7, 6, 4, 33, 2333, 0};
int length = 13;

int main() {

  int mysteryNum;

  printf("type a number: ");
  scanf("%d",&mysteryNum);

  int i;
  for (i = 0; i <= length; i ++)
    {

      int target = forbiddenList[i];

      if (mysteryNum == target)
        {
          printf("You have chosen of the forbidden list!\n");
          printf("YOU SHALL NOT PASS!!\n");
        }
    }
  return 0;
}

er... haven't done c... ever... you should take C++...

止于盛夏 2024-10-28 21:58:20
int preconditions[] = { 4,5,78 }; // it should be in most likely order
int i = 0;
for(;i<3;++i) {
   if( x == preconditions[i] ) {
      // code here.
   }
}
int preconditions[] = { 4,5,78 }; // it should be in most likely order
int i = 0;
for(;i<3;++i) {
   if( x == preconditions[i] ) {
      // code here.
   }
}
鲜血染红嫁衣 2024-10-28 21:58:20

语法是:

if(condition1 || condition2 || condition3 || ...){
    // Do something
} else {
    // Do something
}

回答你的问题:

if( (x == 5) || (x == 4) || (x == 78) ){
   //do something
} else {
  //do something
}

The syntax is:

if(condition1 || condition2 || condition3 || ...){
    // Do something
} else {
    // Do something
}

Answer to your question:

if( (x == 5) || (x == 4) || (x == 78) ){
   //do something
} else {
  //do something
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文