While loops · Loop While
Loop while a condition is true
- A
whileloop repeats as long as its condition is true. - It checks the condition before each pass.
- Use it when you do not know the number of repeats in advance.
Ulangi selama kondisi benar
- Perulangan
whileberulang selama kondisinya benar. - Ia memeriksa kondisi sebelum setiap putaran.
- Gunakan ini ketika Anda tidak tahu jumlah ulangan sebelumnya.
Always change the condition
- Something inside the loop must move toward making the condition false.
- If it never becomes false, the loop runs forever (an infinite loop).
- Press Stop if a loop ever gets stuck.
Selalu ubah kondisi
- Sesuatu di dalam perulangan harus bergerak menuju kondisi yang salah.
- Jika tidak pernah menjadi salah, perulangan berjalan selamanya (perulangan tak terbatas).
- Tekan Stop jika perulangan macet.
count = 3
while count > 0:
print(count)
count = count - 1
break and continue
breakstops the loop right away.continueskips the rest of this pass and goes to the next one.while True:loops forever until abreakends it.
break dan continue
breakmenghentikan perulangan segera.continuemelewatkan sisa putaran ini dan beralih ke putaran berikutnya.while True:berulang selamanya hinggabreakmengakhiri.
n = 0
while True:
n = n + 1
if n == 4:
break
print(n)
Checking before or after
- A
whileloop checks first, so it may run zero times. - A "repeat until" loop checks after, so it always runs at least once.
- Python has no
repeat; usewhile True:with abreakat the end.
Memeriksa sebelum atau setelah
- Perulangan
whilememeriksa pertama, sehingga mungkin berjalan nol kali. - Perulangan "repeat until" memeriksa sesudah, sehingga selalu berjalan minimal sekali.
- Python tidak memiliki
repeat; gunakanwhile True:denganbreakdi akhir.
In Cambridge pseudocode
whileis the pre-condition loop;REPEAT … UNTILis the post-condition loop.
Dalam pseudocode Cambridge
whileadalah perulangan pra-kondisi;REPEAT … UNTILadalah perulangan pasca-kondisi.
Count ← 3
WHILE Count > 0
OUTPUT Count
Count ← Count - 1
ENDWHILE
REPEAT
OUTPUT Count
Count ← Count + 1
UNTIL Count = 3
Common mistakes
- Change something inside the loop so the condition can become false, or it runs forever.
- A
whilechecks its condition first, so it may run zero times.
Kesalahan umum
- Ubah sesuatu di dalam perulangan agar kondisi bisa menjadi salah, atau akan berjalan selamanya.
- A
whilememeriksa kondisinya terlebih dahulu, sehingga mungkin berjalan nol kali.
Now you try
- Make sure your condition will eventually become false.
- Stuck on the loop? Do the short drill Finish the countdown first. It is one missing line.
- Press Check answer to test your code.
Sekarang Anda coba
- Pastikan kondisi Anda akhirnya menjadi salah.
- Macet di perulangan? Lakukan latihan singkat Finish the countdown terlebih dahulu. Itu adalah satu baris yang hilang.
- Tekan Periksa jawaban untuk menguji kode Anda.
A while loop builds up a value · Perulangan while membangun nilai
A while loop repeats until its condition is false. · Perulangan while mengulang hingga kondisinya bernilai false.
Use a while loop to count down. Each turn, print the value of count, then make count one smaller. Stop when it reaches 0, so the output is 5, 4, 3, 2, 1 — one per line. · Gunakan perulangan while untuk menghitung mundur. Setiap putaran, cetak nilai count, lalu kurangi count satu langkah. Berhenti saat mencapai 0, sehingga outputnya adalah 5, 4, 3, 2, 1 — satu per baris.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Use a while loop to add up the numbers 1 to 10. Store the total in total (the answer is 55). · Gunakan perulangan while untuk menjumlahkan angka dari 1 hingga 10. Simpan total dalam total (jawabannya 55).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Start n at 1 and add 1 each pass. The loop stops too soon: it ends while n is still 1. Change the condition so it keeps going while n * n is at most 50. When it stops, n is the first whole number whose square is over 50. · Mulai n dari 1 dan tambahkan 1 setiap kali melewati loop. Loop berhenti terlalu cepat: berakhir saat n masih 1. Ubah kondisinya agar terus berjalan selama n * n paling banyak 50. Saat berhenti, n adalah bilangan bulat pertama yang kuadratnya lebih besar dari 50.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Same answer, with break. n starts at 1 inside while True:. The if is already inside the loop and n grows after it. Change the condition so you break when n * n is greater than 50. Leave the answer in n (it is 8). Do not add 1 before the test — that makes the answer 9. · Jawaban sama, dengan break. n dimulai dari 1 di dalam while True:. Perhitungan if sudah ada di dalam loop dan n bertambah setelah perhitungan tersebut. Ubah kondisinya agar Anda break ketika n * n lebih besar dari 50. Biarkan jawaban dalam n (yaitu 8). Jangan tambahkan 1 sebelum pengujian — itu akan membuat jawabannya menjadi 9.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Start with n = 1 and steps = 0. While n is smaller than 1000, double n and add 1 to steps. When the loop ends, n is 1024. How many doublings did it take? Leave the answer in steps. · Mulai dengan n = 1 dan steps = 0. Selama n lebih kecil dari 1000, gandakan n dan tambahkan 1 ke steps. Saat loop berakhir, n bernilai 1024. Berapa banyak penggandaan yang dibutuhkan? Biarkan jawaban dalam steps.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.