
Yamu
今回は繰り返し
処理while文
を学びます
合わせて読みたい |
【アルゴリズム】pythonで繰り返し(for文)の処理を学ぶ |
繰り返し処理とは ?
繰り返しは特定の操作や処理を何度も
実行するための仕組みを指します
whileループは条件ベースの繰り返しです


Yamu
while処理は
変数の初期値が
条件式を満たしているか
が重要です。
満たしていれば
処理に入る
満たしていなければ
処理に入らない
また最後に
変数増減を自身で
定義しないと
無限ループに
なってしまいます
While文
whileループは条件ベースの繰り返しです
初期値が条件式を満たす時処理に入ります
i = 2
while i < 1000 :
print(f"{i}^2 = {i**2}")
i = i ** 2
実行結果

表を使って処理を確認します
処理 | 条件式 | i |
i = 2 | 2 | |
while(i < 1000) | 2 < 1000 T | |
i = i **2 | 4 | |
while(i < 1000) | 4 < 1000 T | |
i = i **2 | 16 | |
while(i < 1000) | 16 < 1000 T | |
i = i **2 | 256 | |
while(i < 1000) | 256 < 1000 T | |
i = i **2 | 65536 | |
while(i < 1000) | 65536 < 1000 F |

Yamu
iの初期値2は
<1000なので真
従って処理に入ります
処理を進めていって
i = 65536になった時
1000以上なので
処理を抜けます