[英]else if & curly brackets
為什么這兩個代碼段具有不同的輸出?
它們之間的唯一區別是每個if / else-if語句周圍的花括號,但這在這里不重要,對吧?
while (1){
if (i>=n&&j<0)
break;
else if (j<0)
if (Arr[i])
c++;
else if (i>=n)
if(Arr[j])
c++;
else if (Arr[i]==1&&Arr[j]==1)
c+=2;
i++;
j--;
}
..
while (1){
if (i>=n&&j<0){
break;
}
else if (j<0){
if (Arr[i])
c++;
}
else if (i>=n){
if(Arr[j])
c++;
}
else if (Arr[i]==1&&Arr[j]==1){
c+=2;
}
i++;
j--;
}
如果您正確格式化了第一個代碼段
while (1){
if (i>=n&&j<0)
break;
else if (j<0)
if (Arr[i])
c++;
else if (i>=n)
if(Arr[j])
c++;
else if (Arr[i]==1&&Arr[j]==1)
c+=2;
i++;
j--;
}
那么可以看到else或else if對應於最接近的if語句。
解析器將else-branch
與最接近的if
,因此它將解析一個表達式,例如
if(a) if(b) c; else d;
如
if(a) {if(b) c; else d;}
而不是
if(a) {if(b) c;} else d;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.