C++
int | 整數 |
---|---|
double | 浮點數 |
char | 一個字 ex: "a"、"@" |
string | 字串 |
bool | 布林值ex : true/false |
符號 | 意義 |
---|---|
! | 反敘述 |
&& | and |
I I | or |
std::cout << "要輸出的內容" ;
std::cin >> "要輸入的內容" ;
int main() {
int a,b;
for(int i=0; i < 5; i++) {
cin >> a;
cout << a << endl;
cin >> b;
cout << "旗子已被立起" << endl;
}
}
輸入: a (b會沒辦法輸入)
輸出:
0
旗子已被立起
0
旗子已被立起
0
旗子已被立起
0
旗子已被立起
0
旗子已被立起
解決方法:建立一個當輸入錯誤型態時會進入的迴圈,例如:
while (cout << "輸入 = " && !(cin >> a)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "輸入無效,請重新輸入";
}
int number = 2;
switch(number) {
case 1:
//要做的事
std::cout << " " ;
break;
case 2:
//要做的事
std::cout << " " ;
break;
}
if(grade == 9) {
std::cout << " " ;
}
else if (grade == 10){
std::cout << " " ;
}
else {
std::cout << " " ;
}
if,else能夠以三元運算子表示(a ? b : c)
ex : if(x == true) val =y;
else val =z;
能夠被寫成:
val = x ? y :z;
while( A.條件式 ) {
B.當條件成立時,就重覆做的事…
}
int i = 0 ;
int n = 10 ;
while(i<n) {
std::cout << " ";
i++;
}
for( [A]一開始先做什麼事 ; [B]條件式 ; [D]等C每作完一次,就做什麼事 ) {
[C]當B條件成立時,就重覆做的事…
}
for (int i=99;i>0;i--) {
cout << i << "bottles of pop on the wall." << endl;
cout << "Take one down and pass it around." << endl;
cout << i-1 << "bottles of pop on the wall." << endl;
}
輸出陣列常用單行的for迴圈來印出,ex:
for(int i=0; i < sizeof(array)/sizeof(*arr); i++){
cout << arr[i] << endl;
}
也可以利用for range的寫法
for(int i : arr ) {
cout << i << endl;
}
需要注意的是,傳進 for range 的需為一個能夠使用begin()與end()語法的容器,在使用時可以將型態換為auto,就不需要自行判斷型態了,但最好還是了解一下型態,否則出錯時會檢查不出個所以然。
下面將介紹幾種For迴圈的變化形式
第一種是在初始條件運算式中使用兩個或更多的變數來控制迴圈,兩個控制變數之間是以逗點","來區隔:
#include <iostream>
using namespace std;
int main() {
int x, y;
for ( x = 0, y = 0; x + y <= 10; x++ ) {
cout << "請輸入y:";
cin >> y;
cout << "x+y = " << x + y << endl;
}
}
每一次迴圈循環,x都會遞增一,y值都會初始化為0,不要忘記這裡XD 因為在迴圈循環時,for迴圈會先執行繼續條件運算式,也就是在使用者輸入y值之前,程式就要先判斷 x+y<=10 是不是對的,如果y沒有初始為0,可能導致意料外的結果。
第二種是將for陳述中的遞增運算式去除,因為在某些情況下它可能是多餘的,例如下面這個迴圈:
for (int x = 0; x != 123; ) {
cin >> x;
}
可以看見這裡的目的是當輸入123時要停止迴圈,也就是說不需要遞增運算式。
我們還可以把初始條件運算式寫在迴圈外部,例如:
int x = 0;
for ( ; x != 123; ) {
cin >> x;
}
但雖然這樣的寫法仍合於語法,但我們並不鼓勵使用,因為它叫不具有結構性,會讓程式流於鬆散。
第三種是for迴圈形式的無限迴圈,我們只需要讓for陳述裡的三個運算式全都忽略不寫,就會得到一個永遠都不會結束的迴圈,例如:
for(;;){
cout << "永不停止的迴圈" << endl;
}
事實上這就等於while(1)的迴圈,需要再手動break掉迴圈。
第四種是沒有body的for迴圈,這個迴圈不執行任何事。那這樣的迴圈能有什麼作用呢? 它可以產生延遲,在一些特殊場合是很有用的技巧(如工業控制),如:
for(int t = 0; t < value; t++);
value通常會是一個long int,譬如我們設value = 10000000,則這個迴圈會空轉1000萬次,由於每一次循環都必須執行兩個運算式 t<value 和 t++ ,因此會花掉一些執行時間,對整個程式而言,就如同時間延遲的作用。
label:{}
goto label;
#include <iostream>
using namespace std;
int main() {
for (int i = 0 ; i < 10 ; i ++ ) {
cout << endl << " i= " << i << " and j= ";
for (int j = 0 ; j < 10 ; j ++ ) {
cout << j << " ";
if (i == 5) {
cout << endl;
goto here ;
}
}
}
cout << "測試";
for (int i = 3 ; i < 10 ; i ++ ) {
cout << "i= " << i << "and j= ";
for (int j = 4 ; j < 10 ; j ++ ) {
cout << j << " | ";
if (i == 5) {
cout << endl;
goto here2 ;
}
}
}
here :{
cout << "stop 1 !!";
}
here2 :{
cout << "stop 2 !!";
}
}
實際輸出後會發現,cout << "測試"; 與第二個for迴圈都沒有被執行,這是因為當第一個迴圈的i==5時,進入了if,因此吃到了goto here; 導致程式直接跳到了here的標籤處而我們也可以發現here2這個標籤下的cout << "stop 2 !!"; 也被執行了,這是因為標籤在沒有被觸發的情況下是沒有作用的,因此於標籤裡的程式和沒在標籤裡的程式是沒有差別的。
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing