Review 1

洪翠憶

Feedback

大家的回應(。・∀・。)ノ♥

Awesome Dude!

全對的人:D

Array

Declare an Array

宣告陣列

int arr[3] = {6, 8, 9};

陣列型態    陣列名稱    空間格數        初始值

0

1

2

6

8

9

Declare an Array

宣告陣列

int a[5];
//只宣告,未設定初始值

int b[5] = {2, 4};
//可以不用打完所有的初始值,未打的會自動初始化(0、''、...)

int c[] = {0, 1, 2};
//完整設定完每個初始值時可省略陣列大小

Take Value in Array

取陣列的值

std::cout << arr[2];

陣列名稱    空間索引值

0

1

2

6

8

9

Assign Value to Array

給陣列中的元素值

arr[0] = 7;

陣列名稱    空間索引值        值

0

1

2

6

8

9

7

Loop

for vs while

for(int i=0;i<5;i++){
    std::cout << i << ' ';
}
//0 1 2 3 4 

宣告變數    條件(為true時才會跑迴圈)    改變變數值

int i=0
while(i<5){
    std::cout << i << ' ';
    i += 1;
}
//0 1 2 3 4 

條件(為true時才會跑迴圈)

break vs continue

i++ vs ++i

int a = 1;
std::cout << a++ << ' ';
std::cout << a << '\n';

int b = 1;
std::cout << ++b << ' ';
std::cout << b << '\n';

i++:先輸出後+1

++i:先+1後輸出

上星期的Kahoot!

Q5

a[0] a[1] a[2] a[3] a[4]
0 2 4 6 8

補充內容

一樣是基本語法,只是前幾堂課有點趕講不完,所以現在才補充回去

Condition

switch(變數){
    case 值:
        陳述句;
        break;
    case 值:
        陳述句;
        break;
    default:
        陳述句;
        break;
}

3

9

3

case

case

default

9

8

switch - case

條件式 ? true的回傳值 : false的回傳值

ternary operator

?:

//舉例:

int my_wives = 2147483647;
std::string hmm = my_wives > 1 ? "wow DD" : "good";

//hmm的值是"wow DD"

Loop

do{
	陳述句;
}while(條件式);

do - while

記得打分號

  • 先跑{}內的程式,再檢查while的條件是否符合

Array

int a[2][3] = {{0, 1, 2}, {3, 4, 5}};
int b[2][3] = {0, 1, 2, 3, 4, 5};
//二維陣列(2*3)

int c[2][3][2] = {{{0, 1}, {2, 3}, {4, 5}}, {{6, 7}, {8, 9}, {10, 11}}};
//三維陣列(2*3*2)

Multidimesional Array

a[0][0] a[0][1] a[0][2]
a[1][0] a[1][1] a[1][2]
0 1 2
3 4 5

review1

By justhentai

review1

  • 386