简体   繁体   English

如何确定哪个条件为真?

[英]How to determine which condition was true?

Say I have a few conditions inside a if statement:假设我在if语句中有几个条件:

if(x < 0 || x > 10 || x == 5)
{

}

If either x is greater than 10 or less than 0 or equal to 5, I want the program to stop working.如果x大于 10 或小于 0 或等于 5,我希望程序停止工作。

if(x < 0 || x > 10 || x == 5)
{
    stop(); // PSEUDO CODE
}

However , I want the program to say something depending on which condition was true.但是,我希望程序根据哪个条件为真说出一些话。 Something like this:像这样的东西:

if(x < 0 || x > 10 || x == 5)
{
    if(x < 0)
    {
        printf("your number was less than 0");
    }

    if(x > 10)
    {
        printf("your number was greater than 10");
    }

    if(x == 5)
    {
        printf("wow, your number is equal to 5!");
    }

    stop(); // PSEUDO CODE
}

That's a horrible way of doing it because it unnecessarily checks for the conditions twice;这是一种可怕的做法,因为它不必要地检查了两次条件; how can I do the same in a more efficient way?我怎样才能以更有效的方式做同样的事情?

Why make things complicated?为什么要把事情复杂化? Just remove the outer check, and put stop() inside both of the inner ones:只需删除外部检查,并将stop()放入两个内部检查:

if(x < 0)
{
    printf("your number was less than 0");
    stop();
}

if(x > 10)
{
    printf("your number was greater than 10");
    stop();
}

You can even use an else if if you want. else if

Save the boolean result in a variable and reuse it inside.将 boolean 结果保存在一个变量中并在其中重用它。 Redundancy is ok sometimes.冗余有时是可以的。 Just check/count the number of executions if it's possible to be lessen down.如果可以减少,只需检查/计算执行次数。

bool isXNegative = x < 0;
bool isXMoreThan = x > 10;
bool isXFive = (x == 5);

//for specific conditions
if(isXNegative)
{
    printf("your number was less than 0");
}

if(isXMoreThan)
{
    printf("your number was greater than 10");
}

if(isXFive)
{
    printf("wow, your number is equal to 5!");
}

//for combination of the conditions.
if(isXNegative || isXMoreThan || isXFive)
{
    //common code here for all the conditions.
    stop(); // PSEUDO CODE
}
//else if(another condition combination here...){...}
//else {...}

You can set a flag in the failure cases, then check the flag afterward to do the cleanup.您可以在失败情况下设置一个标志,然后检查该标志以进行清理。

int do_stop = 0;
if(x < 0)
{
    printf("your number was less than 0");
    do_stop = 1;
}

if(x > 10)
{
    printf("your number was greater than 10");
    do_stop = 1;
}

if(x == 5)
{
    printf("wow, your number is equal to 5!");
    do_stop = 1;
}

if (do_stop) {
    stop();
}

Assuming you do want to be complicated, save some keystrokes and take advantage of short-circuiting, you can assign expression results to variables in the if condition like so:假设您确实想变得复杂,节省一些击键并利用短路,您可以将表达式结果分配给if条件中的变量,如下所示:

  int c1, c2, c3;
  if ((c1 = x < 0) || (c2 = x > 10) || (c3 = x == 5)) {
    if (c1) {
      printf("your number was less than 0\n");
    } else if (c2) {
      printf("your number was greater than 10\n");
    } else {
      printf("wow, your number is equal to 5!\n");
    }
  }

Just be aware that if the condition short-circuits (say x < 0 ), then the value of c2 and c3 is not initialized.请注意,如果条件短路(例如x < 0 ),则c2c3的值不会被初始化。

I don't actually advocate writing your code this way, but this language feature can be useful and I didn't see it mentioned.我实际上并不提倡以这种方式编写代码,但是这种语言功能可能很有用,我没有看到它被提及。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM