# 第三堂社課 --- ## 複習 ---- ## C++的架構 ```cpp= #include<iostream> using namespace std; int main(){ //你的主程式寫在這裡 } ``` ---- ## 資料型別 | 名稱 | 儲存值 | 例子 | | -------- | -------- | -------- | | int | 整數 | 1 | | float | 浮點數(小數) | 1.1 | | bool | 布林值(1或0) | true | | char | 字母 | a | | string | 字串 | XiaoChengYi | ---- ## 變數的宣告與定義 ```cpp= #include<iostream> using namespace std; int main(){ int a; int b=2; int c=3,d=4; double e=3.1415926; char f='f'; bool g=true; } ``` ---- ## 算術運算子 | 運算子 | 用途 | 範例 | |:------:| :----: | :----: | | + | 將左值加上右值 | ```a=3+2``` | | - | 將左值減去右值 | ```a=3-2``` | | * | 將左值乘上右值 | ```a=3*2``` | | / | 將左值除以右值 | ```a=3/2``` | | % | 取左值除以右值的餘數 | ```a=3%2``` | ---- ## 算術運算子的範例 ```cpp= #include<iostream> using namespace std; int main(){ int a=10; int b=a+2;//b=12 int c=a-2;//c=8 int d=a*2;//d=20 int e=a/2;//e=5 int f=a%2;//f=0 } ``` ---- ## 指定運算子 | 運算子 | 用途 | |:------:| :----: | | += | 將變數值加上右值再賦予於變數 | | -= | 將變數值減去右值再賦予於變數 | | *= | 將變數值乘上右值再賦予於變數 | | /= | 將變數值除以右值再賦予於變數 | | %= | 取變數值除以右值的餘數賦予於變數 | ---- ## 指定運算子的範例 ```cpp= #include<iostream> using namespace std; int main(){ int a=10,b=10,c=10,d=10,e=10; a+=2;//a=12; b-=2;//b=8 c*=2;//c=20 d/=2;//d=5 e%=2;//e=0 } ``` ---- ## 輸入輸出 ```cpp= #include<iostream> using namespace std; int main(){ int a,b; cin>>a>>b; //使用萃取運算子>>連結cin與各個要輸入的變數名稱 cout<<a<<'+'<<b<<'='<<a+b; //使用插入運算子連結cout與各個要輸出的值 } ``` ---- ## 關係運算子 | 符號 | 意義 | 例子 | | :--------: | :--------: | :--------: | | > | 大於 | 2>1(true) | | < | 小於 | 2<1(false) | | >= | 大於等於 | 2>=1(true) | | <= | 小於等於 | 2<=1(false) | | == | 等於 | 2==1(false) | | != | 不等於 | 2!=1(true) | ---- ## 邏輯運算子 | 符號 | 意義 | 例子 | | :--------: | :--------: | :--------: | | && | 且 | 2>1&&2<1(false) | | \|\| | 或 | 2>1\|\|2<1(true) | | ! | 相反 | !(2<1)(true)| ---- ## if...else...判斷式 ```cpp= if(條件1){ //陳述 } else if(條件2){ //陳述 } else{ //陳述 } ``` ---- ## switch...case...判斷式 ```cpp= switch (變數/運算式) { case 值1: //陳述 break; case 值2: //陳述 break; default: //陳述 break;//可省略 } ``` --- ## 複習小測驗 請登入zerojudge,在進入我們社課的課程就可以看到一個複習小測驗,前三名有獎勵喔! --- ## 迴圈 ---- ## 迴圈的意義 當相同效果的程式重複很多遍時,將他濃縮成一個可以一直重複的邏輯,便於更好的去撰寫與理解程式 ---- ## 從1輸出到100(不用迴圈) ```cpp= #include<iostream> using namespace std; int main(){ cout<<1<<2<<3<<4<<5<<6<<7<<8<<9<<10<<11<<12<<13<<14<<15<<16<<17<<18<<19<<20<<21<<22<<23<<24<<25<<26<<27<<28<<29<<30<<31<<32<<33<<34<<35<<36<<37<<38<<39<<40<<41<<42<<43<<44<<45<<46<<47<<48<<49<<50<<51<<52<<53<<54<<55<<56<<57<<58<<59<<60<<61<<62<<63<<64<<65<<66<<67<<68<<69<<70<<71<<72<<73<<74<<75<<76<<77<<78<<79<<80<<81<<82<<83<<84<<85<<86<<87<<88<<89<<90<<91<<92<<93<<94<<95<<96<<97<<98<<99<<100; } ``` 實測用手打,真的很麻煩 ---- ## 從1輸出到100(用迴圈) ```cpp= #include<iostream> using namespace std; int main(){ for(int i=1;i<=100;i++)cout<<i; } ``` 簡潔 有力 好懂 --- ## 遞增(```++```)和遞減(```--```)運算子 ---- ## 遞增(```++```)和遞減(```--```)運算子 當我們想要使某個整數變數遞增或遞減時,我們可以用a=a+1或a=a-1,更簡潔的,也可以用a+=1以及a-=1,但遞增與遞減運算子更為簡潔明瞭:a++、++a和a- -、- -a。 ---- ## 遞增(```++```)與遞減(```--```)運算子的範例 ```cpp= #include<iostream> using namespace std; int main(){ int a=1;//a=1 a++; cout<<a;//a=2 ++a; cout<<a;//a=3 a--; cout<<a;//a=2 --a; cout<<a;//a=1 } ``` ---- ## 前置(```++a```)與後置(```a++```)的區別 其實沒啥區別。 但在運算上會有些許的不同: ```cpp= int a,count=5; a=count+++5;//a=10 /* a+=count+5;//count=5,a=5+5=10 ++count;//count=5+1=6 */ ``` ```cpp= int a,count=5; a=++count+5;//a=11 /* ++count;//count=5+1=6 a+=count+5//count=6,a=6+5=11 */ ``` ---- ## 前置(```++a```)與後置(```a++```)的區別 但如果加上了括號就又不一樣了: ```cpp= int a,count=5; a=(count++)+5;//a=11 /* 先執行括號內的內容 ++count;//count=5+1=6 a+=count+5//count=6,a=6+5=11 */ ``` ---- ## 遞增與遞減運算子的應用 遞增與遞減運算子最常出現的地方莫過於迴圈了,而很少使用在運算裡面,所以剛剛的部分其實不太理解也不太需要去理解,只要避免這樣寫就可以了(我寫題目到現在從來沒遇到過一定要這樣寫的題目) --- ## for 迴圈 ---- ## 基本語法 ```cpp= for (初始;判斷;運算) { 陳述句; } ``` 初始:只在進入迴圈時執行 判斷:判斷是否繼續迴圈 運算:每次一個迴圈循環後執行 ---- ## 範例 ```cpp= #include<iostream> using namespace std; int main(){ for(int i=0;i<10;i++){ cout<<i<<endl; } } ``` ---- ## 範例(更簡潔) ```cpp= #include<iostream> using namespace std; int main(){ for(int i=0;i<10;i++)cout<<i<<endl; } ``` **請注意:只有陳述句為一行時,大括號{}才可以省略** --- ## while 迴圈 ---- ## 基本語法 ```cpp= while(判斷) { 陳述句; } ``` 判斷:判斷是否繼續迴圈 ---- ## 範例 ```cpp= #include<iostream> using namespace std; int main(){ int i=0; while(i<10){ cout<<i++<<endl; } } ``` ---- ## 範例(更簡潔) ```cpp= #include<iostream> using namespace std; int main(){ int i=0; while(i<10)cout<<i++<<endl; } ``` **請注意:只有陳述句為一行時,大括號{}才可以省略** --- ## do...while 迴圈 ---- ## 基本語法 ```cpp= do{ 陳述句; }while(判斷); ``` 判斷:判斷是否繼續迴圈 ---- ## 範例 ```cpp= #include<iostream> using namespace std; int main(){ int i=0; do{ cout<<i++<<endl; }while(i<10); } ``` --- ## 略過迴圈迭代 ---- ## 略過迴圈迭代 當有一個迴圈你不想要執行時 你可以使用```continue```來跳過 ---- ## 範例 ```cpp= #include<iostream> using namespace std; int main(){ for(int i=0;i<10;i++){ if(i==4)continue;//不會輸出 4 cout<<i<<endl; } } ``` --- ## 中斷迴圈 ---- ## 略過迴圈迭代 當有一個迴圈之後的迴圈你都想要跳過時 你可以使用```break```來跳過 ---- ## 範例 ```cpp= #include<iostream> using namespace std; int main(){ for(int i=0;i<10;i++){ if(i==4)break;//4(含)以後的數字都不會輸出 cout<<i<<endl; } } ``` --- ## 巢狀敘述 ---- ## 巢狀敘述 當你將多個條件判斷式或迴圈一層一層組合在一起 即稱之為**巢狀敘述** ---- ## 巢狀if...else...敘述範例 ```cpp= #include<iostream> using namespace std; int main(){ int a; cin>>a; if(a<=1000){ if(a<=500){ if(a<=200){ if(a<=100){ if(a<=50){ if(a<=20){ if(a<=10){ if(a<=5){ if(a<=1){ cout<<1; } else{ cout<<5; } } else{ cout<<10; } } else{ cout<<20; } } else{ cout<<50; } } else{ cout<<100; } } else{ cout<<200; } } else cout<<500; } else{ cout<<1000; } } else{ cout<<2000; } } ``` ---- ## 巢狀迴圈敘述範例 ```cpp= #include<iostream> using namespace std; int main(){ int n; cin>>n; for(int i=1;i<=n;i++){ for(int j=1;j<=i;j++){ cout<<'*'; } cout<<'\n'; } } ``` ---