PS,1880后程序員

          看不完的牙,寫不完的程序,跑不完的步。
          隨筆 - 97, 文章 - 34, 評論 - 10, 引用 - 0
          數據加載中……

          C++ Primer 之 讀書筆記 第六章

           

           

          switch

          switch語句我用的的確不多,還是習慣寫成if-else的形式。

          switch 中的控制流:

          It is a common misunderstanding to expect that only the statements associated with the matched case label are executed. However, execution continues across case boundaries until the end of the switch statement or a break is encountered.

          存在一個普遍的誤解:以為程序只會執行匹配的 case 標號相關聯的語句。實際上,程序從該點開始執行,并跨越 case 邊界繼續執行其他語句,直到 switch 結束或遇到 break 語句為止。

          2switch常見的錯誤寫法:

          l           case 標號必須是整型常量表達式。

          l           如果兩個 case 標號具有相同的值,同樣也會導致編譯時的錯誤。

          Switch內部的變量定義:

          對于 switch 結構,只能在它的最后一個 case 標號或 default 標號后面定義變量。

           

          do while語句

          用作條件判斷的變量一定要定義在循環體之外,

          // repeatedly ask user for pair of numbers to sum

          string rsp; // used in the condition; can't be defined inside the do

          do {

             // ...

          } while (!rsp.empty() && rsp[0] != 'n');

          do while 循環不可以采用如下方式定義變量:

          // error: declaration statement within do condition is not supported

          do {

              // ...

              mumble(foo);

          } while (int foo = get_foo()); // error: declaration in do condition

          不過其實自己寫代碼時,這個各色的寫法一般是不會出現的。

           

          Using the Preprocessor for Debugging(使用預處理器進行調試)

          預處理器還定義了其余四種在調試時非常有用的常量:

          __FILE__ 文件名

          __LINE__ 當前行號

          __TIME__ 文件被編譯的時間

          __DATE__ 文件被編譯的日期

          斷言assert是一種預處理宏,assert 宏就求解條件表達式,如果結果為 false,assert 輸出信息并且終止程序的執行。如果該表達式有一個非零(例如,true)值,則 assert 不做任何操作。

           

          posted on 2009-05-20 14:21 amenglai 閱讀(236) 評論(0)  編輯  收藏 所屬分類: C++ Primer 之 讀書筆記

          主站蜘蛛池模板: 吉林省| 仲巴县| 聂拉木县| 南澳县| 田东县| 鹿泉市| 金昌市| 南通市| 涞源县| 夏津县| 新蔡县| 阳春市| 墨玉县| 澎湖县| 伊春市| 砀山县| 绥化市| 观塘区| 兰坪| 桐乡市| 灌南县| 台湾省| 和政县| 天全县| 宜黄县| 融水| 昆明市| 新津县| 隆化县| 独山县| 彰武县| 康定县| 浑源县| 健康| 民县| 合江县| 铜梁县| 根河市| 巴林右旗| 凉城县| 张家港市|