DAY 4
0

## # while循環

### 語法：

``````while(condition)
{
statement(s);
}
``````

statement(s) 可能是單個語句或語句塊，condition 可以是任何的情況。

### 流程圖：

ex.

``````int main() {

// 一開始的年齡為 10
int age = 10;

while (age < 20) {

NSLog(@"value of age: %d",age);

age++; //age = age + 1
}

return 0;
}
``````

``````2021-09-13 01:43:26.071385+0800 TestOC[98218:2703900] value of age: 10
2021-09-13 01:43:26.074459+0800 TestOC[98218:2703900] value of age: 11
2021-09-13 01:43:26.074598+0800 TestOC[98218:2703900] value of age: 12
2021-09-13 01:43:26.074709+0800 TestOC[98218:2703900] value of age: 13
2021-09-13 01:43:26.074848+0800 TestOC[98218:2703900] value of age: 14
2021-09-13 01:43:26.074956+0800 TestOC[98218:2703900] value of age: 15
2021-09-13 01:43:26.075059+0800 TestOC[98218:2703900] value of age: 16
2021-09-13 01:43:26.075151+0800 TestOC[98218:2703900] value of age: 17
2021-09-13 01:43:26.075238+0800 TestOC[98218:2703900] value of age: 18
2021-09-13 01:43:26.075315+0800 TestOC[98218:2703900] value of age: 19
``````

## # for循環

### 語法：

``````for ( init; condition; increment )
{
statement(s);
}
``````
1. 一開始先執行`init`，這個步驟可以聲明或初始化任何循環變量，也可以不把聲明放這裡，只要一個分號出現。
2. 條件進行判斷，如果為`true`，執行 statement。若為`false`，直接離開循環。
3. 主要的循環執行後，控制流跳到 increment 語句，該語句允許更新任何循環控制變量。這語句可以留空，只要一個分號出現。
4. 再次判斷條件，如果為`true`，重複過程；若為`false`，循環終止。

### 流程圖：

ex.

``````#import <Foundation/Foundation.h>

int main() {

// 一開始的年齡為 10
int age;

for (age = 10; age < 20; age++) {
NSLog(@"value of age: %d",age);

}

return 0;
}
``````

``````2021-09-13 08:26:46.041283+0800 TestOC[98565:2723233] value of age: 10
2021-09-13 08:26:46.041812+0800 TestOC[98565:2723233] value of age: 11
2021-09-13 08:26:46.041891+0800 TestOC[98565:2723233] value of age: 12
2021-09-13 08:26:46.041971+0800 TestOC[98565:2723233] value of age: 13
2021-09-13 08:26:46.042048+0800 TestOC[98565:2723233] value of age: 14
2021-09-13 08:26:46.042123+0800 TestOC[98565:2723233] value of age: 15
2021-09-13 08:26:46.042202+0800 TestOC[98565:2723233] value of age: 16
2021-09-13 08:26:46.042890+0800 TestOC[98565:2723233] value of age: 17
2021-09-13 08:26:46.043247+0800 TestOC[98565:2723233] value of age: 18
2021-09-13 08:26:46.043450+0800 TestOC[98565:2723233] value of age: 19
``````

## # do...while循環

### 語法：

``````do
{
statement(s);

}while( condition );
``````

### 流程圖：

ex.

``````#import <Foundation/Foundation.h>

int main() {

// 一開始的年齡為 10
int age = 10;

do {
NSLog(@"value of age: %d",age);

age ++;

} while (age < 20);

return 0;
}
``````

``````2021-09-13 10:25:54.404092+0800 TestOC[99161:2767015] value of age: 10
2021-09-13 10:25:54.404767+0800 TestOC[99161:2767015] value of age: 11
2021-09-13 10:25:54.404909+0800 TestOC[99161:2767015] value of age: 12
2021-09-13 10:25:54.405042+0800 TestOC[99161:2767015] value of age: 13
2021-09-13 10:25:54.405207+0800 TestOC[99161:2767015] value of age: 14
2021-09-13 10:25:54.405303+0800 TestOC[99161:2767015] value of age: 15
2021-09-13 10:25:54.405408+0800 TestOC[99161:2767015] value of age: 16
2021-09-13 10:25:54.405508+0800 TestOC[99161:2767015] value of age: 17
2021-09-13 10:25:54.405619+0800 TestOC[99161:2767015] value of age: 18
2021-09-13 10:25:54.405706+0800 TestOC[99161:2767015] value of age: 19
``````

## # break語句

1. 循環語句：當遇到`break`，循環立即終止。
2. 可以用來終止switch語句的case情況（之後會提到）

### 用法：

``````break;
``````

### 流程圖：

ex.

``````#import <Foundation/Foundation.h>

int main() {

// 一開始的年齡為 10
int age = 10;

while (age < 20) {

NSLog(@"value of age: %d",age);

age ++;

if (age > 15) {
break;
}

}

return 0;
}
``````

`age > 15`就會結束這循環。

``````2021-09-13 11:00:35.905066+0800 TestOC[99396:2786775] value of age: 10
2021-09-13 11:00:35.908529+0800 TestOC[99396:2786775] value of age: 11
2021-09-13 11:00:35.908625+0800 TestOC[99396:2786775] value of age: 12
2021-09-13 11:00:35.908711+0800 TestOC[99396:2786775] value of age: 13
2021-09-13 11:00:35.908797+0800 TestOC[99396:2786775] value of age: 14
2021-09-13 11:00:35.908871+0800 TestOC[99396:2786775] value of age: 15
``````

## # continue語句

continue語句的工作原理有點像 break語句，差別在於`break`強制終止整個循環`continue`強制終止當前循環，但還在循環內。

### 用法：

``````continue;
``````

### 流程圖：

ex.

``````#import <Foundation/Foundation.h>

int main() {

// 一開始的年齡為 10
int age = 10;

do {
age ++;

if (age == 15) {

NSLog(@"Hello, world");
continue;
}

NSLog(@"value of age: %d",age);

} while (age < 20);

return 0;
}
``````

``````2021-09-15 17:17:21.479747+0800 TestOC[81114:955281] value of age: 11
2021-09-15 17:17:21.480617+0800 TestOC[81114:955281] value of age: 12
2021-09-15 17:17:21.480724+0800 TestOC[81114:955281] value of age: 13
2021-09-15 17:17:21.480826+0800 TestOC[81114:955281] value of age: 14
2021-09-15 17:17:21.480923+0800 TestOC[81114:955281] Hello, world
2021-09-15 17:17:21.481009+0800 TestOC[81114:955281] value of age: 16
2021-09-15 17:17:21.481085+0800 TestOC[81114:955281] value of age: 17
2021-09-15 17:17:21.481182+0800 TestOC[81114:955281] value of age: 18
2021-09-15 17:17:21.481303+0800 TestOC[81114:955281] value of age: 19
2021-09-15 17:17:21.481643+0800 TestOC[81114:955281] value of age: 20
``````

`value of age: 15`被跳過了。

## # 無限循環

``````#import <Foundation/Foundation.h>

int main() {

for ( ; ; ) {
NSLog(@"This loop will run forever.");

}

return 0;
}
``````