DAY 7
1
Software Development

## 條件控制

``````var now = true;

if(now) {
console.log('天氣好棒！');
} else {
console.log('天氣好差！');
}
``````

``````var myName = 'Ray';

if(myName === 'QQ') {
console.log('哈囉 QQ');
} else if (myName === 'Ray') {
console.log('哈囉 Ray');
} else if(myName === 'Hello') {
console.log('哈囉');
} else {
console.log('都不熟');
}
``````

``````now = True
if now:
print('天氣好棒！')
else:
print('天氣好差！')
``````

(注意 Python 中並沒有三等號運算子，在 JavaScript 中會有這個功能主要原因是隱含轉型導致，但在 Python 不會有這個狀況，通常你必須自己轉型。)

``````myName = 'Ray'

if myName == 'QQ':
print('哈囉 QQ')
elif myName == 'Ray':
print('哈囉 Ray')
elif myName == 'Hello':
print('哈囉')
else:
print('都不熟')
``````

``````myName = 'Ray'

match myName:
case 'QQ':
print('哈囉 QQ')
case 'Ray':
print('哈囉 Ray')
``````

``````now = True
if (now):
print('天氣好棒！')
else:
print('天氣好差！')
``````

``````print((1))
``````

## 迴圈

``````for(var i = 0; i < 10; i+=1){
console.log(i); // 0...1...2......9
}
``````

``````for i in range(10):
print(i)
``````

• `range(10)` - 產生 0~9 的範圍
• `range(2, 10)` - 產生 2~9 的範圍
• `range(2, 10, 4)` - 產生遞增範圍 4，，以這個範例來講起始值是 2，因此會 2 + 4 = 6，但是因為 6 + 4 超過 `range()` 範圍，因此只會出現 2 跟 6。

``````for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
print(i)
``````

``````for i in 'Ray':
print(i)
``````

``````var i = 0;
while(i < 5) {
i += 1;
console.log('Hello Ray' + i);
}
``````

``````var i = 0;
do {
i += 1;
console.log('Hello Ray' + i);
} while(i < 5)
``````

``````var i = 0;
do {
i += 1;
console.log('Hello Ray' + i);
} while(i < 0); // Hello Ray1

var i = 0;
// 條件不符合不執行
while(i < 0) {
i += 1;
console.log('Hello Ray' + i);
}
``````

``````i = 0
while i < 5:
i += 1
print('Hello Ray' + str(i))
``````

``````i = 0
status = True

while status:
i += 1
print('Hello Ray' + str(i))
if i >= 0:
status = False
``````

``````i = 0
while True:
print(i)
i += 1
``````

## 九九乘法表 Python 答案

``````for i in range(2, 10):
for j in range(2, 10):
print(str(i) + 'x' + str(j) + '=' + str(i*j))
``````