Iteration — loops · Iterasi — loop
Count-controlled loops
- Iteration means repeating steps.
- A count-controlled loop repeats a fixed number of times.
- In Python that is
for i in range(n):.
Perulangan terkontrol hitungan
- Iterasi berarti mengulang langkah-langkah.
- Perulangan terkontrol hitungan mengulang sejumlah kali yang tetap.
- Di Python itu adalah
for i in range(n):.
for i in range(5):
print("Hello")
Choosing the range
range(5)gives0, 1, 2, 3, 4.range(1, 6)gives1, 2, 3, 4, 5— it stops before 6.range(2, 11, 2)counts in steps of 2.
Memilih rentang
range(5)menghasilkan0, 1, 2, 3, 4.range(1, 6)menghasilkan1, 2, 3, 4, 5— berhenti sebelum 6.range(2, 11, 2)menghitung dengan langkah 2.
for n in range(1, 6):
print(n)
Pre-condition loops: while
- A pre-condition loop tests before each pass.
- It may run zero times if the test is false at the start.
Perulangan pra-kondisi: while
- Perulangan prasyarat menguji sebelum setiap putaran.
- Bisa berjalan nol kali jika tes bernilai salah di awal.
count = 0
while count < 5:
print(count)
count = count + 1
Post-condition loops: REPEAT UNTIL
- A post-condition loop tests after each pass, so it always runs at least once.
- Python has no
repeat, so we usewhile Truewithbreak.
Perulangan pasca-syarat: ULANGI SAMPAI
- Perulangan pasca-syarat menguji setelah setiap putaran, sehingga selalu berjalan minimal sekali.
- Python tidak memiliki
repeat, jadi kami menggunakanwhile Truedenganbreak.
total = 0
while True:
total = total + 1
if total >= 3:
break
print(total)
Each pass changes count · Setiap kali lewat mengubah count
A loop repeats its body; count must change so the test finally fails. · Loop mengulang isinya; count harus berubah agar tes akhirnya gagal.
In the exam: FOR, WHILE, REPEAT
- Pseudocode has all three loop types.
Dalam ujian: FOR, WHILE, REPEAT
- Pseudocode memiliki ketiga jenis perulangan tersebut.
FOR I ← 1 TO 5
OUTPUT I
NEXT I
Common mistakes
- A
forloop repeats a known number of times; awhilerepeats until something changes. range(n)runs0ton - 1.- Change the condition inside a
while, or it never ends.
Kesalahan umum
- Perulangan
formengulang sejumlah kali yang diketahui;whilemengulang sampai ada perubahan. range(n)menjalankan0hinggan - 1.- Ubah kondisi di dalam
while, atau ia tidak akan pernah berakhir.
Now you try
- Pick the loop that fits each task.
- Press Check answer to test your code.
Sekarang Anda coba
- Pilih perulangan yang sesuai untuk setiap tugas.
- Tekan Periksa jawaban untuk menguji kode Anda.
Use a for loop to print the numbers 1 to 5, one on each line. · Gunakan loop for untuk mencetak angka 1 sampai 5, satu per baris.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Use a loop to add up the numbers 1 to 10, then print the total (it is 55). · Gunakan perulangan untuk menjumlahkan angka 1 hingga 10, lalu cetak totalnya (hasilnya 55).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Read a whole number n, then count down from n to 1, one per line. For input 3, print 3, 2, 1. · Baca bilangan bulat n, lalu hitung mundur dari n ke 1, satu per baris. Untuk input 3, cetak 3, 2, 1.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.