我讀寫成C++代碼強制括號
for (kf=0; kf<nf; kf++)
if (EPS_MOCK[kf] == 1)
for (i=0; i<nptsx; i++)
for (j=0; j<nptsz; j++)
{
x0[iv] = log(inv_Controls->epsilonBed.GetElem(j,i,kf));
iv = iv+1;
}
for (kf=0; kf<nf; kf++)
if (inv_num_packman[kf] == -1)
for (i=0; i<nzx; i++)
{
x0[iv] = log(inv_Controls->num_packman[i+kf*nzx]);
iv = iv+1;
}
else if (inv_num_packman[kf] == 1)
{
x0[iv] = log(inv_Controls->num_packman[kf*nzx]);
iv = iv+1;
}
;而將括號中的設置將
for (kf=0; kf<nf; kf++){
if (EPS_MOCK[kf] == 1){
for (i=0; i<nptsx; i++){
for (j=0; j<nptsz; j++)
{
x0[iv] = log(inv_Controls->epsilonBed.GetElem(j,i,kf));
iv = iv+1;
}
}
}
}
for (kf=0; kf<nf; kf++){
if (inv_num_packman[kf] == -1){
for (i=0; i<nzx; i++){}
{
x0[iv] = log(inv_Controls->num_packman[i+kf*nzx]);
iv = iv+1;
}
}
else if (inv_num_packman[kf] == 1)
{
x0[iv] = log(inv_Controls->num_packman[kf*nzx]);
iv = iv+1;
}
}
是第一個版本正確的一段代碼?這裏的括號只是爲了更好的可讀性還是強制編譯器理解代碼的邏輯?
真的,你應該瞄準儘可能寫出最可讀的代碼。第一種形式等同於第二種形式對於許多人來說可讀性較差。 – shuttle87