# 直接上菜

``````       #
##
###
####
#####
######
#######
########``````

# 範例

• 輸入
``3``
• 輸出
``````  #
##
###``````

# 範例

• 輸入
``5``
• 輸出
``````    #
##
###
####
#####``````

# 如果這樣做...

``````int n;
std::cin >> n;

if (n == 3) {
std::cout << "  *" << std::endl
<< " **" << std::endl
<< "***" << std::endl;
} else if (n == 5) {
std::cout << "    *" << std::endl
<< "   **" << std::endl
<< "  ***" << std::endl
<< " ****" << std::endl
<< "*****" << std::endl;
}``````

# 如果這樣做...

• 輸入
``6``
• 輸出
``````     #
##
###
####
#####
######``````

# While Loop

``````while (/* condition */) {
/* body */
}``````
``````當 (/* 條件為真 */) {
/* 執行程式 */
}``````

# While Loop

``````int i = 1;
while (i <= 10) {
std::cout << i << std::endl;
i++;
}``````

# While Loop

``````int i = 2;
int sum = 0;

while (i <= 100) {
sum += i;
i += 2;
}

std::cout << sum << std::endl;``````

# While Loop

``````int i = 1;
while (i <= 100) {
std::cout << i << std::endl;
}``````
``````int i = 1;
while (i > 0) {
std::cout << i << std::endl;
i++;
}``````

# While Loop

``````while (true) {
/* run forever */
}``````
``````while (1) {
/* run forever */
}``````

# 離開迴圈

break: 常見寫法

``````while (/* condition */) {
/* body */
if (/* another condition */)
break;
}``````

# 離開迴圈

break: 範例

``````int answer = 127;
int guess;
while (true) {
std::cout << "Enter a number" << std::endl;
std::cin >> guess;
std::cout << "You've got it!" << std::endl;
break;
} else {
std::cout << "Wrong..." << std::endl;
}
}``````

# 繼續迴圈

continue: 常見寫法

``````while (/* condition */) {
/* body 1 */
if (/* another condition */)
continue;
/* body 2 */
}``````

# 繼續迴圈

continue: 範例

``````int a = 0;
int b = 0;
while (true) {
std::cin >> a >> b;
if (b == 0)
continue;
std::cout << "a / b = " << a / b << std::endl;
}``````

# Do-While Loop

``````do {
/* body */
}
while (/* condition */);``````

# Do-While Loop

do-while: 範例

``````int answer = 127;
int number = 0;

do {
std::cout << "Enter a number" << std::endl;
std::cin >> number;
}

std::cout << "number is 127" << std::endl;``````

# Nested Loop

``````int i = 1;
while (i < 10) {
int j = 1;
while (j < 10) {
std::cout << i << " * " << j << " = " << i * j << " ";
j++;
}
std::cout << std::endl;
i++;
}``````

# Back to Mario

• 輸入
``3``
• 輸出
``````  #
##
###``````

n
n

n

# Back to Mario

``````int n;
std::cin >> n;

int i = 1;
while (i <= n) {
/* 印出第i層 */
i++;
}``````

n

# Back to Mario

• 第一層
``  #``
• 第二層
`` ##``
• 第三層
``###``

i
n-i
n-i
i
i

# Back to Mario

``````int j = 0;
while (j < n - i) {
std::cout << " ";
j++;
}
j = 0;
while (j < i) {
std::cout << "#";
j++;
}
std::cout << std::endl;``````

i

# Back to Mario

``````int n;
std::cin >> n;

int i = 1;
while (i <= n) {
int j = 0;
while (j < n - i) {
std::cout << " ";
j++;
}
j = 0;
while (j < i) {
std::cout << "#";
j++;
}
std::cout << std::endl;
i++;
}``````

# 執行了幾次？

``````int n = 10;
while (n--) {
std::cout << n << std::endl;
}``````
``````int n = 10;
while (--n) {
std::cout << n << std::endl;
}``````

By JT

• 620