[コース5.4]ループステートメント:whileループ
実行ステートメントは、単一のステートメントまたはステートメントブロックです。
判定条件は任意の式にすることができ、ゼロ以外またはnull以外の値はすべてtrueです。
判定条件が偽の場合ループを終了します。
1.基本的な操作ロジック
count = 0
while count < 9:
print( 'The count is:', count)
count = count + 1
print( "Good bye!")
# 这里count<9是一个判断语句,当判断为True时,则继续运行
----------------------------------------------------------------------
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!
2.無限ループについて:条件判断文が常に真の場合、ループは無期限に実行されます
var = 1
while var == 1 :
num = input("Enter a number :")
print( "You entered: ", num)
print( "Good bye!")
# 该条件永远为true,循环将无限执行下去
# 一定要避免无限循环!!
----------------------------------------------------------------------
3.while-else语句
count = 0
while count < 5:
print(count, " is less than 5")
count = count + 1
else:
print(count, " is not less than 5")
# 逻辑和if-else一样
----------------------------------------------------------------------
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5