The loop that never ends · Perulangan yang tidak pernah berhenti
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.
Loop yang tidak pernah berakhir
Jalankan ini terlebih dahulu dan perhatikan apa yang terjadi. Baris yang membuat count menjadi lebih kecil ada di sana — tetapi berada di luar loop, sehingga tidak pernah dijalankan.
Dalam Python, indentasi adalah strukturnya: baris yang disisipkan di bawah while milik loop dan berulang, baris yang kembali ke margin kiri muncul setelah loop dan dijalankan sekali di akhir. Di sini loop tidak pernah mencapai baris terakhirnya, sehingga pertanyaannya tetap true selamanya. Halaman web akan menyadarinya dan menghentikannya untuk Anda, tetapi program Anda harus berhenti sendiri.
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. Move it inside so the program prints 5, 4, 3, 2, 1 and stops. · Jalankan ini dulu dan lihat apa yang terjadi. Baris yang membuat count lebih kecil ada di sana — tapi berada di luar perulangan, jadi tidak pernah dijalankan. Pindahkan ke dalam agar program mencetak 5, 4, 3, 2, 1 dan berhenti.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.