Skip to content · ⁨コンテンツへスキップ⁩

The loop that never ends · ⁨永遠に終わらないループ⁩

English

The loop that never ends

Run this first and watch what happens. The line that makes count smaller is there — but it is outside the loop, so it never runs.

In Python the indentation is the structure: lines indented under while belong to the loop and repeat, a line back at the left margin comes after the loop and runs once at the end. Here the loop never reaches its last line, so its question stays true for ever. The page will notice and stop it for you, but your program should stop itself.

日本語

終了しないループ

まずこれを実行して様子を見てください。count を小さくする行は存在しますが、ループの 外側 にあるため、決して実行されません。

Pythonではインデントそのものが構造です:while の下にインデントされた行はループの一部であり繰り返されますが、左端に戻った行はループ後に位置し、最後に一度だけ実行されます。ここではループが最終行に到達できないため、条件は永遠に真のままです。ページはこれに気づいて停止しますが、プログラム自体も停止する必要があります。

Log in or create account · ⁨ログインまたはアカウント作成⁩

IGCSE, A-Level & AP