# C++ 基礎語法 ## 變數型別 | 常用變數型別 | 說明 | 範圍 | |:------------:| ------ | ---------------- | | int | 整數 | -2^31^ ~ 2^31^-1 | | long long | 整數 | -2^63^ ~ 2^63^-1 | | float | 小數 | 精準度7位 | | double | 小數 | 精準度15位 | | bool | 布林值 | `true` `false` | | char | 字元 | `a` `b` `c` | ### 宣告方式 ```cpp= // 宣告變數 int i; long long l; float f; double d; char c; bool b // 初始賦值 int i = 5; long long l = 10L; float f = 2.5f; double d = 3.5Lf; char c = 'c'; bool b = false; ``` ### 其他參考資料 #### [ASCII CODE](https://zh.wikipedia.org/wiki/ASCII) ## 輸入與輸出 ### 引入函式庫、命名空間 #### ```cpp #include<iostream> using namespace std; ``` ### 輸入 #### 數字 ```cpp= #include<iostream> using namespace std; int main() { int n; char c; float f; cin >> n; // 輸入一個整數 cin >> c; // 輸入一個字元 cin >> f; // 輸入一個浮點數 // 也可以縮減成一行 cin >> n >> c >> f; return 0; } ``` #### 整行字串 ```cpp= #include<iostream> #include<string> using namespace std; int main() { string str; getline(cin, str); return 0; } ``` ### 輸出 ```cpp= #include<iostream> using namespace std; int main() { int n = 1; char c = 'a'; float f = 1.5; cout << n; // 1 cout << c; // a cout << f; // 1.5 // 也可以縮減成一行 cout << n << c << f; return 0; } ``` ### 其他參考資料 #### [iomanip](https://www.cplusplus.com/reference/iomanip/) (cout輸出格式) ## 運算子 ### 算術運算子 | 符號 | 用處 | | 符號 | 用處 | |:----:|:------:|:---:|:----:|:----:| | + | 加法 | | - | 減法 | | * | 乘法 | | / | 除法 | | % | 取餘數 | | | | ### 比較運算子 ## 條件判斷 ### if-else #### ```cpp= #include<iostream> using namespace std; int main() { int a = 2; int b = 3; // if if (a > 0) { cout << a; // output 2 } // if-else if (a > b) { cout << a; } else { cout << b; // output 3 } return 0; } ``` ### 巢狀 if-else * if 包含 if #### ```cpp= #include<iostream> using namespace std; int main() { int a = 1, b = 2, c = 3; if (a > b) { if (a > c) { cout << "a is the biggest"; } else { cout << "c is the biggest"; } } else { if (b > c) { cout << "b is the biggest"; } else { cout << "c is the biggest"; } } return 0; } ``` #### output ``` c is the biggest ``` ### 多重 if-else ```cpp= #include<iostream> using namespace std; int main() { int a = 1; if (a == 1) { cout << "數字1"; } else if (a == 2) { cout << "數字2"; } else { cout << "1和2以外的整數"; } return 0; } ``` #### output ``` 數字1 ``` ### switch #### ```cpp= #include<iostream> using namespace std; int main() { int a = 1; // switch 算是多重if-else的改良版 // 依照上面轉換 switch (a) { case 1: cout << "數字1"; break; case 2: cout << "數字2"; break; default: cout << "1, 2以外的整數"; } return 0; } ``` #### output ``` 數字1 ``` ### 三元運算 #### 用途: 縮減程式碼 ##### 使用方式: `<變數> = (條件) ? <條件為true的值> : <條件為false的值>;` ```cpp= int a = 0, b = 2, big; // if 條件式 if (a > b) { big = a; } else { big = b; } // 三元運算 big = a > b ? a : b; // 兩個的結果會相同 -> big = 2 ``` ### 其他注意事項 :::info 如果`case`裡結尾沒加上`break`會無條件執行它以下的所有`case` ::: #### 如果條件只放整數,0為`false`,其他整數皆為`true` ```cpp= int a = 15; if (a) { cout << "a is true"; } else { cout << "a is false"; } ``` ## 迴圈控制 ### for 迴圈 #### 使用時機 * 清楚知道迴圈需要跑幾次的時候 #### 使用方式 ```cpp= for (初始化;條件式;執行完的動作) { // do something } ``` * 初始化:進入迴圈的第一個動作 (可為空) * 條件式:決定迴圈是否繼續跑下去的條件 (可為空) * 執行完的動作:執行完一次內容後的動作 (可為空) #### 範例 ```cpp= for (int i = 1;i <= 10;i++) { cout << i << ' '; } ``` ##### output ``` 1 2 3 4 5 6 7 8 9 10 ``` ### while 迴圈 #### 使用時機 * 不確定迴圈會跑多少次的時候 #### 使用方式 ```cpp= while (條件式) { // do something } ``` * 條件式:是否繼續執行迴圈的條件式 #### 範例 ```cpp= int n = 17; while (n > 1) { cout << n; if (n%2 == 1) { n = 3*n + 1; } else { n = n / 2; } } ``` ##### output ``` 17 52 26 13 40 20 10 5 16 8 4 2 ``` ### do-while 迴圈 #### 與 while 的差別 * 會先無條件的執行一次 #### 使用方式 ```cpp= do { // do something } while (條件式); ``` * 條件式:是否繼續執行迴圈的條件式 ### break 關鍵字 #### 使用時機 * 想在中途跳出迴圈的時候 #### 範例 ```cpp= for (int i = 1;i <= 10;i++) { if (i == 5) { break; } cout << i << ' '; } ``` ##### output ``` 1 2 3 4 ``` ### continue 關鍵字 #### 使用時機 * 想跳過某次迴圈的時候 #### 範例 ```cpp= for (int i = 1;i <= 10;i++) { if (i == 5) { continue; } cout << i << ' '; } ``` ##### output ``` 1 2 3 4 6 7 8 9 10 ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up