爲什麼當我使用if語句時,程序會給我一個不同的結果。當我使用if語句時,爲什麼程序會給我一個不同的結果
如果我使用else if語句它會打印一個5.然而,如果我更改其他if語句,則會打印出完全不同的圖片。誰能告訴我爲什麼?
#include<iostream>
using namespace std;
// Print 5.
int main() {
int n=5;
for(int row=1;row<=2*n-1;row++)
{
for(int col=1;col<=n;col++)
{
if(row==1||row==n||row==2*n-1)
cout<<"*";
else if (col==1&&row<n||col==n&&row>n)
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
return 0;
}
我一直以爲如果和其他人一樣。
if'了'if'只計算當包含'if'條件是錯誤的...它改變了事情(c1){} else if(c2){}'等價於if(c1){} if(!c1 && c2){}'。 –