天天看點

《C++遊戲程式設計入門(第4版)》——2.3 使用else子句include

本節書摘來自異步社群出版社《c++遊戲程式設計入門(第4版)》一書中的第2章,第2.3節,作者:【美】michael dawson(道森),更多章節内容可以通路雲栖社群“異步社群”公衆号檢視。

c++遊戲程式設計入門(第4版)

在if語句中加入else子句可以引入隻有當被驗證的表達式為false時才執行的代碼。下面給出包含else子句的if語句的形式:

// score rater 2.0

// demonstrates an else clause

using namespace std;

int main()

{

   int score;

   cout << "enter your score: ";

   cin >> score;

   if (score >= 1000)

   {

     cout << "you scored 1000 or more. impressive!n";

   }

   else

     cout << "you scored less than 1000.n";

   return 0;

}<code>`</code>

我們已經看到if語句的第一部分,它的工作方式和以往一樣。如果score大于1000,則顯示消息you scored 1000 or more. impressive!。

   }<code>`</code>

繼續閱讀