我知道這段代碼不能像預期的那樣工作。這段代碼只是快速尋找,我們認爲返回值應該是1,但在執行它返回返回3在開關中不正確的多個案例不會產生編譯器錯誤
// incorrect
variable = 1;
switch (variable)
{
case 1, 2:
return 1;
case 3, 4:
return 2;
default:
return 3;
}
,並有一些正確的選項要做到這一點:
// correct 1
variable = 1;
switch (variable)
{
case 1: case 2:
return 1;
case 3: case 4:
return 2;
default:
return 3;
}
或
// correct 2
switch (variable)
{
case 1:
case 2:
return 1;
case 3:
case 4:
return 2;
default:
return 3;
}
部分回答我願意想知道爲什麼不正確的形式編譯時沒有錯誤或甚至警告(至少在Borland C++編譯器中)。
編譯器在該代碼中的理解是什麼?
值的事實[如何逗號操作符的工作?](HTTP://計算器。 com/questions/54142 /如何做這個逗號操作工) –