# 課程筆記
## 主程式
```cpp=
#include <iostream> //控制輸入輸出的標頭檔,如cin、cout
#include <cmath> //為複雜的數學運算
using namespace std; //為定義基本的函式的基本命名空間
int main(){ //為主程式開始的地方
cout << pow(4,3) << endl; //64
cout << sqrt(16) << endl; //4
cout << log(10,100) << endl; //2
cout << sin(0) << endl; //0
cout << cos(0) << endl; //1
return 0;//告訴電腦程式OK
}
```
## 變數定義
為什麼要告訴電腦我們要定義變數,因為我們要**正確的分配給每個變數**。如果變數只有整數,就只要分配整數的記憶體給電腦儲存資料,而有小數點的話,就必須要分配儲存小數點的位置。
| 資料型態 | 中文名稱 | 範例 |
| -------- | -------- | -------- |
| int(integer) | 整數 | 1、1000、-3 |
|float|浮點數(小數點)|1.0、2.33、-5.2|
|char|字元|'A'、'你' (字元記得要以單引號進行括號)|
|bool|布林|0、1 (用來判斷對錯,1為對,0為錯)|
| 算數運算子 |意義|
| -------- | -------- |
|+|加|
|-|減|
|*|乘|
|/|除|
|%|取餘數|
|()|括號,優先順序第一|
|++|遞增加一|
|-\-|遞減減一|
**算數運算子計算順序:括號先算,先乘除,後加減**
## 關係運算子
|關係運算子|意義|
| -------- | -------- |
|< |小於關係 |
|<= |小於等於關係|
|> |大於關係 |
|>= |大於等於關係|
|==|等於關係|
|!=|不等於關係|
## 邏輯運算子
|邏輯運算子|意義||
| -------- | -------- |--------|
|\|\| |or 關係 |當一個對的時候全部邏輯都是對的|
|&&|and 關係| 當一個是錯的時候全部邏輯都是錯的|
## if else 判斷式
**if else** 是一個連續判斷對錯的判別式,且非常依照順序執行。
而如何合理的使用呢?
1.要用if作為判斷式的開頭,若是用 **else if** 或是 **else** 來作為開頭則無法執行
2.if else 判斷式的執行順序為**if -> else if(數量不等,可以不寫) -> else (數量至多一個,可以不寫)**
**if判斷式**
```cpp=
if(條件){
條件「成立」時的程式碼
}
```
**if判斷式示範**
```cpp=
#include<iostream>
using namespace std;
int main(){
int a=5;
if(a!=6){
cout << "a不等於6" << endl;
}
return 0;
}
```
**if else if 多重選擇判別式**
```cpp=
if(條件1){
條件1「成立」時程式碼區塊
}
else if(條件2){
條件1「不成立」,且條件2「成立」時的程式碼
}
else if(條件3){
條件1、2「不成立」,且條件3「成立」時的程式碼
}
.
.
.
else{
上述條件都「不成立」時的程式碼
}
```
**if else if 多重選擇判別式示範**
```cpp=
#include<iostream>
using namespace std;
int main(){
int i = 2;
if (i == 3){//這個if為獨立的判別式
cout << "i = 3\n";
}
if (i == 1){//這個if 和下面的else if 為一對判別式
cout << "i = 1\n";
else if(i == 2){
cout << "i = 2\n";
}
return 0;
}
```
## for & while 迴圈
for & while 基本上是**重複的迴圈結構**,當你需要執行**重複性高**的事情,就可以使用上述工具
**for 迴圈**
```cpp=
for(第一次進入前做的事;條件判斷;迴圈每執行一次要做的事){
程式碼
}
```
**for 程式碼示範**
```cpp=
#include<iostream>
using namespace std;
int main(){//輸出0到9
for(int i = 0; i < 10; i++){
cout << i << endl;
}
return 0;
}
```
**while 語法**
```cpp=
while(條件判斷){ // 條件為「真」的時候繼續執行
程式碼
}
```
**while 程式碼示範**
```cpp=
#include<iostream>
using namespace std;
int main(){//輸出從0到9
int i = 0; //第一次進入迴圈前想做的事
while(i < 10){ //若條件為true則進入迴圈,反之則跳過迴圈執行之後的部分
cout << i << endl; //迴圈內想要做的事
i++;
}
return 0;
}
```
## break & continue
```cpp=
#include<iostream>
using namespace std;
int main(){
int b=8;
while(b <= 9){
if(b != 9){
break;
//若偵測到即立刻跳出目前的迴圈(while & for迴圈),不繼續執行
}
}
cout << "我跳出while迴圈了" << endl;//輸出此字串
return 0;
}
```
```cpp=
#include<iostream>
using namespace std;
int main(){
int a=0;
int b=8;
while(b <= 9){
if(a == 0){
a++;
continue;
//若偵測到即立刻忽略這一次的迴圈(while & for),執行下一次迴圈
}
b = 10;
}
return 0;
}
```
## 陣列
當變數可以儲存一個**資料(整數、小數、字元、布林)**,但是如果同時要儲存多筆資料的時候就顯得非常麻煩,所以當我們**統一名稱**來作為儲存位置,我們就不需要定義這麼多變數。
在初始化時可以決定要放置什麼類型的資料型態,與python串列不同的地方是,陣列裡面不可以存放不同資料型態的東西

**陣列的輸入輸出**
```cpp=
#include<iostream>
using namespace std;
int main(){
int money[4]={10000,20000,30000,40000}
cout << money[2];//30000
cin >> money[1];
for(int i=0 ; i<=3 ; i++ ){
cout << money[i];//輸出陣列裡面的所有東西
}
return 0;
}
```
**不同形式的陣列**
```cpp=
#include<iostream>
using namespace std;
int main(){
float number[3]={1.25,2.4568,3.15876};
char word[3]={'a','b','c'};
string name[3]={"漢堡","薯條","雞塊"};
return 0;
}
```
## String字串
在我們前面的程式碼我們可以看到,當我們用**雙引號(" ")** 括起來之後,裡面的字就會是我們的字串,當我們平常說話的時候,說出來的一整句話,在程式當中就會是代表一串字串,因此string 就可以進行輸入字串以及輸出字串。
```cpp=
#include<iostream>
using namespace std;
int main(){
string name;//定義資料型態
cin >> name; //輸入字串(注意輸入時中間不要打空格)
cout << name << endl;
name = "qjfdfj";
cout << name << endl;
}
```