Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
If statement 编写以下条件的最有效方法是什么_If Statement_Optimization - Fatal编程技术网

If statement 编写以下条件的最有效方法是什么

If statement 编写以下条件的最有效方法是什么,if-statement,optimization,If Statement,Optimization,我有一些代码如下所示: if (a == 0 or b == 0) { if (c == true) { return 0 else if (a == 0) a = 1 c = true else if (b == 0) b = 1 c = true } } 在不必再次检查a或b中哪一个等于0的情况下,编写此代码的最有效方法是什么?这可能已经得到了回答,但我不知道人们会怎么问这个问题

我有一些代码如下所示:

if (a == 0 or b == 0) {
    if (c == true) {
        return 0
    else if (a == 0)
        a = 1
        c = true
    else if (b == 0)
        b = 1
        c = true
    }
}

在不必再次检查a或b中哪一个等于0的情况下,编写此代码的最有效方法是什么?这可能已经得到了回答,但我不知道人们会怎么问这个问题。

您可以尝试压缩并删除一些不必要的
if/else
块:

if ((a == 0 && c == true) || (b == 0 && c == true)) {
  // Since we return here, we don't need the if/else blocks after this
  return 0;
}

if (a == 0) {
  a = 1;
} else if (b == 0) {
  b = 1;
}

// If we reach this point, c is always true regardless of a or b
c = true;

<>但一般来说,代码的效率几乎保持不变,所以你也可能要考虑可读性。

< P>你绝对不需要最后的<代码>(b=0)< /C> < /P>
这是什么语言?添加适当的语言标记。代码的其余部分在哪里?为什么一个分支返回而其他分支不返回?
if (a == 0 or b == 0) {
    if (c == true) {
        return 0
    else if (a == 0)
        a = 1
        c = true
    else
        b = 1
        c = true
    }
}