大括號 { }
內的東西往右推,可以層次分明、釐清邏輯。
排版:在{ }
內的東西利用 Tab鍵 向右推。
if( 條件 )
{
如果條件成立時做什麼...
}
else
{
否則做什麼...
}
#include<iostream>
using namespace std;
int main()
{
int grade;
cin >> grade;
if( grade >= 60 )
{
cout << "You Pass!" << endl;
}
else
{
cout << "You Fail..." << endl;
}
return 0;
}
71
You Pass!
20
You Fail...
當 { }
內的東西只有一列的時候,可以省略大括號。
看起來會較為美觀,但是初學者建議還是都加上大括號,避免混淆。
#include<iostream>
using namespace std;
int main()
{
int grade;
cin >> grade;
if( grade >= 60 )
cout << "You Pass!" << endl;
else
cout << "You Fail..." << endl;
return 0;
}
71
You Pass!