搞懂Python程序中的while循环
while condition:
statement_block
number = 50
sum = 0
while number > 0:
sum = sum + number;
number = number - 1;
print("1 到 50 之和为: %d" % (sum))
1 到 50 之和为: 1275
while condition:
statement_block else: condition:
statement_block
number = 3
while number > 0:
print(number)
number = number - 1;
else:
print("number小于等于0")
3
2
1
number小于等于0
number = 50
while number > 0: print("number 大于 50。")
number 大于 50。
number 大于 50。
number 大于 50。
number 大于 50。
number 大于 50。
number 大于 50。
......
number = 50
while number > 0:
print("number 大于 50。")
break;
number 大于 50。
number = 5
while number > 0:
number = number - 1;
if number == 3:
continue;
print(number)
4
2
0
1