if-else序列

时间:2022-09-02 14:52:22

I' using the following logic for testing whether the triangle is isosceles, equilateral, scalene or right angled.

我用下面的逻辑来测试三角形是否为等腰三角形,等边三角形,斜角或直角三角形。

if (side1 == side2 || side2 == side3 || side1 == side3)
    printf("Isosceles triangle.");
else if (side1 == side2 && side2 == side3 && side3 == side1)
    printf("equilateral triangle");

I get the output for sides 3 3 3 as isosceles but not equilateral but when I interchange the logic that is write the logic of equilateral first I get equilateral. I can't understand what's happening?

我得到了3 3 3边的输出作为等腰线但不是等腰线但是当我交换逻辑,也就是先写出等腰线的逻辑时,我得到了等腰线。我不明白发生了什么事?

5 个解决方案

#1


4  

You shouldn't use else in this case.

在这种情况下你不应该用else。

Code:

代码:

if (condition)
     code
else if (condition2)
     code2

Checks if condition is true. If so it executes code. Only if condition is false, condition2 is checked and code2 could be executed.

检查条件是否为真。如果是,则执行代码。只有当条件为false时,才检查condition2并执行code2。

#2


3  

Your code "checks" the second if only when the first if is false.
logically the second if can be true only if the first if is true...

您的代码“检查”第一个if是false时的第二个if。逻辑上,如果第一个if为真,则第二个if为真…

I would change the code to:

我将把代码改为:

if (side1 == side2 || side2 == side3 || side1 == side3)
{
    printf("Isosceles triangle.");
    if (side1 == side2 && side2 == side3 && side3 == side1)
        printf("equilateral triangle");
}

#3


2  

else is executed only if if is not executed. Just remove else and it will be able to print both statements in such case.

else只在if未执行时才执行。只需删除else语句,它就可以在这种情况下打印两个语句。

#4


2  

Side1 = 3, side2 = 3, side = 3, which means that side1 == side2 is true. That's why your program print out "Iosceles". Beacuse first if is true, second won't be checked. It would be only if the first one was false.

边1 = 3,边2 = 3,边3,这意味着边1 ==边2为真。这就是为什么你的程序打印出“Iosceles”。因为如果第一个为真,第二个将不被检查。只有第一个是假的。

#5


1  

Just switch the order of the if statements. Since every equilateral triangle is isosceles, you never make it into the elseif. Make your code read like this:

只需切换if语句的顺序。因为每个等边三角形都是等腰三角形,所以你永远不能把它变成等腰三角形。让您的代码如下所示:

if (side1 == side2 && side2 == side3 && side3 == side1)
    printf("Equilateral triangle");
else if (side1 == side2 || side2 == side3 || side1 == side3)
    printf("Isosceles triangle.");

Alternatively, you can nest the equilateral if block inside the isosceles if block if you want both results printed:

或者,如果您想打印两个结果,您可以将等边if块嵌到等腰if块中:

if (side1 == side2 || side2 == side3 || side1 == side3){
    if (side1 == side2 && side2 == side3 && side3 == side1){
        printf("Equilateral triangle");
    }
    printf("Isosceles triangle.");
}

Another optimization to consider is that your equilateral check only needs two check to equalities. ie:

另一个需要考虑的优化是,您的等边检查只需要对等式进行两次检查。即:

(side1 == side2 && side2 == side3) => (side1 == side3)

So, the if statement can read like this:

if语句是这样的

if (side1 == side2 && side2 == side3)

#1


4  

You shouldn't use else in this case.

在这种情况下你不应该用else。

Code:

代码:

if (condition)
     code
else if (condition2)
     code2

Checks if condition is true. If so it executes code. Only if condition is false, condition2 is checked and code2 could be executed.

检查条件是否为真。如果是,则执行代码。只有当条件为false时,才检查condition2并执行code2。

#2


3  

Your code "checks" the second if only when the first if is false.
logically the second if can be true only if the first if is true...

您的代码“检查”第一个if是false时的第二个if。逻辑上,如果第一个if为真,则第二个if为真…

I would change the code to:

我将把代码改为:

if (side1 == side2 || side2 == side3 || side1 == side3)
{
    printf("Isosceles triangle.");
    if (side1 == side2 && side2 == side3 && side3 == side1)
        printf("equilateral triangle");
}

#3


2  

else is executed only if if is not executed. Just remove else and it will be able to print both statements in such case.

else只在if未执行时才执行。只需删除else语句,它就可以在这种情况下打印两个语句。

#4


2  

Side1 = 3, side2 = 3, side = 3, which means that side1 == side2 is true. That's why your program print out "Iosceles". Beacuse first if is true, second won't be checked. It would be only if the first one was false.

边1 = 3,边2 = 3,边3,这意味着边1 ==边2为真。这就是为什么你的程序打印出“Iosceles”。因为如果第一个为真,第二个将不被检查。只有第一个是假的。

#5


1  

Just switch the order of the if statements. Since every equilateral triangle is isosceles, you never make it into the elseif. Make your code read like this:

只需切换if语句的顺序。因为每个等边三角形都是等腰三角形,所以你永远不能把它变成等腰三角形。让您的代码如下所示:

if (side1 == side2 && side2 == side3 && side3 == side1)
    printf("Equilateral triangle");
else if (side1 == side2 || side2 == side3 || side1 == side3)
    printf("Isosceles triangle.");

Alternatively, you can nest the equilateral if block inside the isosceles if block if you want both results printed:

或者,如果您想打印两个结果,您可以将等边if块嵌到等腰if块中:

if (side1 == side2 || side2 == side3 || side1 == side3){
    if (side1 == side2 && side2 == side3 && side3 == side1){
        printf("Equilateral triangle");
    }
    printf("Isosceles triangle.");
}

Another optimization to consider is that your equilateral check only needs two check to equalities. ie:

另一个需要考虑的优化是,您的等边检查只需要对等式进行两次检查。即:

(side1 == side2 && side2 == side3) => (side1 == side3)

So, the if statement can read like this:

if语句是这样的

if (side1 == side2 && side2 == side3)