黃祥陞 @Sprout 2024 C/C++語法班
Modified from @Sprout 2021 & 2023
#
##
###
####
#####
######
#######
########
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
#
##
###
####
#####
######
#include <iostream>
using namespace std;
int main () {
int input;
cin >> input;
if (input % 2) {
cout << input << " is odd\n";
}
else {
cout << input << " is even\n";
}
}
簡單啦
???
#include <iostream>
using namespace std;
int main () {
cout << "3 6 9\n";
}
#include <iostream>
using namespace std;
int main () {
if (!(1568963 % 45631)) {
cout << "1568963\n";
}
if (!(1568964 % 45631)) {
cout << "1568964\n";
}
if (!(1568965 % 45631)) {
cout << "1568965\n";
}
if (!(1568966 % 45631)) {
cout << "1568966\n";
}
/* ... */
}
??????????
while (/* condition */) {
/* body */
}
當 (/* 條件為真 */) {
/* 執行程式 */
}
e.g. 印出 1 到 10
int i = 1;
while (i <= 10) {
std::cout << i << std::endl;
i++;
}
e.g. 算出 1~100 的偶數和
int i = 2;
int sum = 0;
while (i <= 100) {
sum += i;
i += 2;
}
std::cout << sum << std::endl;
e.g. 計算輸入是幾位數
int input, cnt = 0;
cin >> input;
while (input) {
input /= 10;
cnt++;
}
std::cout << cnt << std::endl;
注意事項:終止條件
int i = 1;
while (i <= 100) {
std::cout << i << std::endl;
}
int i = 1;
while (i > 0) {
std::cout << i << std::endl;
i++;
}
無窮迴圈
while (true) {
/* run forever */
}
while (1) {
/* run forever */
}
注意事項:生命週期
int cnt = 0;
while (cnt < 5) {
int i = 0;
cnt++;
}
cout << i << '\n'; // error
生命週期的概念也適用於迴圈喔!
每次迴圈宣告的 i
在該輪結束後就不再可用了
do {
/* body */
}
while (/* condition */);
先執行 body,再判斷 condition
e.g. for do-while
int answer = 127;
int number = 0;
do {
std::cout << "Enter a number" << std::endl;
std::cin >> number;
}
while (number != answer);
std::cout << "number is 127" << std::endl;
While vs. Do-While
for (/* init */; /* condition */; /* update */) {
/* body */
}
break: 常見寫法
while (/* condition */) {
/* body */
if (/* another condition */)
break;
}
使用 break 離開當前的迴圈
break: 範例
int answer = 127;
int guess;
while (true) {
std::cout << "Enter a number" << std::endl;
std::cin >> guess;
if (guess == answer) {
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 跳過 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;
}
範例:99乘法表
int i = 1;
while (i < 10) {
int j = 1;
while (j < 10) {
std::cout << i << " * " << j << " = " << i * j << " ";
j++;
}
std::cout << std::endl;
i++;
}
3
#
##
###
觀察:當輸入為 時,總共會有 層
聯想到用迴圈執行 次
int n;
std::cin >> n;
int i = 1;
while (i <= n) {
/* 印出第i層 */
i++;
}
第一個步驟:用一個執行 次的迴圈來印出每一層
下一個步驟:在每一次的迴圈內,印出那一層
#
##
###
觀察:在第 層,有 個#,以及 個空白在前面
用一個 次的迴圈印出空白
接著用 次的迴圈印出#
int j = 0;
while (j < n - i) {
std::cout << " ";
j++;
}
j = 0;
while (j < i) {
std::cout << "#";
j++;
}
std::cout << std::endl;
第二個步驟:利用迴圈印出第 層
下一個步驟:將前後兩個部分連接
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;
}