Label & Loop & Array

Arvin Liu

Label

在程式上的小小提醒點。

Label in general

#include <stdio.h>
int main(){

   /* do somthing */
   Hi_This_is_Label_1:
   /* do something else */
}

Code Line

宣告

#include <stdio.h>
int main(){

   /* do somthing */
   Hi_This_is_Label_1:
   /* do something else */
}

Goto

宣告

#include <stdio.h>
int main(){

   /* do somthing */
   Hi_This_is_Label_1:
   /* do something else */

  goto Hi_This_is_Label_1;
}

Label 在function內是可以直接goto

Exercises - goto 

Infinite Loop

無窮迴圈

#include <iostream>
using namespace std;
int main(){
  OAO:
  cout << "Hi" << endl;
  /*
    DO SOMETHING
  */
  goto OAO;
}

請將 /*DO SOMETHING*/ 處加code,

讓Loop跳出。

Counter

計數器

#include <iostream>
using namespace std;
int main(){
  int i=0;
  OAO:
  i++;
  cout << i << endl;

  if(/* Condition */){
      goto OAO;
  }
}

請將 /*Condition*/ 補完,讓程式從0輸出到100。

DO NOT USE GOTO

除非你很有想法....

Speghitti Code

Loop

就是個迴圈。

A Realistic Example
你媽叫你去掃地

這個時候你會有兩種選擇

  • 真的認真去打掃,乖小孩。
  • 死小孩。

認真打掃

什麼事情也沒有,
繼續你一天的生活。

偷懶玩耍

你媽會叫你再去掃一次。

Event Line

你媽叫你動作去打掃。

打掃要做還是不做

你媽檢查房子,看是要叫你重掃還是滿足了。

宣告

做事

檢查

Code-lize! - goto

Event Line - goto-loop

做事

檢查

宣告

Sweep:
  // do something
  
  if(house_is_not_clean){
  
   
     goto Sweep;
  
  }

goto with condition

label: // 宣告這一件事情
  // do something
  if (條件)
    goto label;

Code-lize! - do-while

很像goto-loop

label: // 宣告這一件事情
  // do something
  if (條件)
    goto label;
do{ // 宣告這一件事情
    // do something
  }while(條件);

Counter: goto vs do-while

#include <iostream>
using namespace std;
int main(){
  int i=0;
  
  OAO:
  i++;
  cout << i << endl;
  if(i < 100){
      goto OAO;
  }
}
#include <iostream>
using namespace std;
int main(){
  int i=0;
  
  do{
    i++;
    cout << i << endl;

  }while(i < 100);
}

goto

do-while

Example - Sigma(1)

請給出N=100時是多少?

\sum^N_{i=1} i
int main(){
  int sum=0, i=0;
  do{
    i++;
    sum += i;
  }while(i < 100);
  cout << sum << endl;
}

請給出N=100時是多少?

Homework

 

一直輸入數字,當所有輸入的數字加總=0時,結束程式整個程式。

1
2
3
-3
-4
1
/*YOU CANNOT TYPE ANYMORE*/

等等...

不是應該要先檢查嘛?

你媽叫你動作去打掃。

打掃要做還是不做

你媽檢查房子,看是要叫你重掃還是滿足了。

宣告

做事

檢查

????

????????????

Event Line - ver.2

你媽檢查房子,
髒了叫你去打掃。

打掃要做還是不做

回去重新檢查就好。

宣告/檢查

做事

回溯

Code-lize! - while

Event Line - while-like

while(house_is_not_clean){

// Do something;

}

宣告/檢查

做事

回溯

while

while(條件){
  // do somthing
}

如果不符合條件

Example - Sigma(2)

Example - while-like

宣告/檢查

做事

回溯

int sum=0, i=1;
while(i<=100){
  sum += i;
  i++;
}
cout << sum << endl;

我們用i在控制迴圈!

在用i控制迴圈時,
哪些必要的呢?

Where is Necessary?

int sum=0, i=1;
while(i<=100){
  sum += i;
  i++;
}
cout << sum << endl;

Change Form - FOR

for - 用;來分成三段!

for(初始狀態;條件;累進運算){
  // do somthing
}

int i=1

;i<=100

;i+=1

int sum=0, i=1;
while(i<=100){
  sum += i;
  i++;
}
cout << sum << endl;
int sum=0;
for(int i=1;i<=100;i+=1){
  sum += i; 
}

累加運算放後面!

int sum=0, i=1;
while(i<=100){
  sum += i;
  i+=1;
}
cout << sum << endl;
\sum^N_{i=1} i

 - while vs for

for

for(初始狀態;條件;累進運算){
  // do something
}

如果不符合條件

int i=0

i<=10000

i+=1

;

;

"for-loop" details

1. 三段都可以省略。

第二段(條件)省略時,預設值是永遠都是True。

while(1){}
for(;;){}

2. i++;

i=i+1; 和 i++; 接近一樣,所以for迴圈通常會像下面這樣

for(int i=0; i<n; i++){}

break / continue

會不會有突發狀況呢?

讓你需要在還沒完成迴圈的時候結束迴圈?

Maybe a solution

int no_fire = true;
while(house_is_not_clean && no_fire){
	// Do something
    if(on_fire){
    	no_fire = false;
    }
}

break: 強制終止迴圈

while(house_is_not_clean && no_fire){
	// Do something
    if(on_fire){
    	break;
    }
}

continue?

for(int i=1; i<10; i++){
  cout << "start " << i << endl;
  if(i%3==0){
    continue;
  }
  cout << "end " << i << endl;
}
for(int i=1; i<10; i++){ 
  cout << "start " << i << endl;
  if(i%3==0){
    break;
  }
  cout << "end " << i << endl;
}
start 1
end 1
start 2
end 2
start 3
start 1
end 1
start 2
end 2
start 3
start 4
end 4
start 5
end 5
start 6
start 7
end 7
start 8
end 8
start 9

Exercise

3n+1 problem

給定一個數字n,請模擬過程。
ex:
 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

9x9 table

1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81

輸出這樣的一個表格。
不要用手打

d649 - 數字三角形

輸入一個數字n,輸出一個靠右的直角三角形
(如右圖)

Array

一種情境:

現在有n隻怪物,你要負責記錄他們的血量。

But...

1. 你不知道n是多少。
2. 就算知道,你可能會這樣寫:

int hp_a, hp_b, hp_c, hp_d, hp_e;
int hp_f, hp_g, hp_h, hp_i, hp_j;
...

So you can...

// 有最多10隻怪物。
int monster_hp[10];
// 你可以用變數monster_hp[0]~monster_hp[9]

def

monster_hp[1] = 10;
monster_hp[1]++;

usage

就跟一般的變數一樣。

Exercise

// 有10隻怪物。
int monster_hp[10];

宣告

將所有怪物的血量都初始成100。

2-D Array

很像是座標平面的東西?

宣告

(0, 0) (0, 1) (0, 2)
(1, 0) (1, 1) (1, 2)
(2, 0) (2, 1) (2, 2)
// 現在有3x3的地圖。
int table[3][3];
// 你可以用變數
// table[0][0]~table[3][3]

def

table[1][1] = 2;
table[1][1] = table[0][0]+1;

usage

還是跟一般的變數一樣。

Made with Slides.com