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 語句為止。 |
有2種switch常見的錯誤寫法:
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 之 讀書筆記