Iteration · Iterasi
Repeat with iteration
- Iteration means doing the same steps again and again.
- Loops save you from writing the same line many times.
- AP CSP cares about loops because they let a little code do a lot of work.
Ulangi dengan iterasi
- Iterasi berarti melakukan langkah-langkah yang sama berulang-ulang.
- Loop menghemat Anda dari menulis baris yang sama berkali-kali.
- AP CSP peduli dengan loop karena memungkinkan sedikit kode melakukan banyak pekerjaan.
Count with for and range
for i in range(n):repeats the bodyntimes.range(5)gives0, 1, 2, 3, 4(it stops before 5).- The loop body is indented, like an
if.
Hitung dengan for dan range
for i in range(n):mengulang tubuhnkali.range(5)memberikan0, 1, 2, 3, 4(berhenti sebelum 5).- Tubuh loop diindentasi, seperti sebuah
if.
for i in range(5):
print(i)
Build a total (accumulation)
- An accumulator is a variable that grows inside a loop.
- Start it at
0before the loop. - Add to it each pass to build a running total.
Bangun total (akumulasi)
- Sebuah akumulator adalah variabel yang bertambah di dalam loop.
- Mulainya dari
0sebelum loop. - Tambahkan ke dalamnya setiap kali lewat untuk membangun total berjalan.
total = 0
for n in range(1, 6):
total = total + n
print(total)
Loop until a condition with while
- A
whileloop repeats as long as its condition isTrue. - Something inside must move toward making it
False. - Use it when you do not know the count in advance.
Loop hingga kondisi dengan while
- Loop
whileberulang selama kondisinyaTrue. - Sesuatu di dalamnya harus bergerak menuju membuatnya
False. - Gunakan ketika Anda tidak mengetahui jumlah sebelumnya.
count = 3
while count > 0:
print(count)
count = count - 1
Loop over a list (for-each)
for item in mylist:visits each item in turn.- The loop variable holds one item per pass.
- This is the clean way to process every value.
Loop melalui daftar (for-each)
for item in mylist:mengunjungi setiap item secara bergantian.- Variabel loop memegang satu item per kali lewat.
- Ini adalah cara bersih untuk memproses setiap nilai.
scores = [10, 20, 30]
for s in scores:
print(s)
In AP CSP pseudocode
- CB has three loop shapes that match Python loops.
REPEAT n TIMES= a fixed count;REPEAT UNTIL(cond)= loop until true.FOR EACH x IN list= a for-each loop.DISPLAY= print,←==,MOD=%.
Dalam pseudocode AP CSP
- CB memiliki tiga bentuk loop yang cocok dengan loop Python.
REPEAT n TIMES= jumlah tetap;REPEAT UNTIL(cond)= loop hingga benar.FOR EACH x IN list= perulangan for-each.DISPLAY= cetak,←==,MOD=%.
REPEAT 5 TIMES
{
DISPLAY "hi"
}
count ← 3
REPEAT UNTIL (count = 0)
{
DISPLAY count
count ← count - 1
}
FOR EACH s IN scores
{
DISPLAY s
}
Common mistakes
range(n)runs0ton - 1.- Change the condition inside a
while, or it never stops.
Kesalahan umum
range(n)menjalankan0hinggan - 1.- Ubah kondisi di dalam
while, atau tidak akan pernah berhenti.
Now you try
- Use a loop in each task.
- Press Check answer to test your code.
Sekarang Anda coba
- Gunakan loop dalam setiap tugas.
- Tekan Periksa jawaban untuk menguji kode Anda.
Iteration repeats the body · Iterasi mengulang isi perulangan
A loop repeats; a counter and a total change each pass. · Sebuah perulangan berulang; sebuah penghitung dan total berubah pada setiap lintasan.
Use a for loop with range(...) to add the whole numbers 1 through 10. Store the result in total. (It should be 55.) · Gunakan perulangan for dengan range(...) untuk menjumlahkan bilangan bulat 1 sampai 10. Simpan hasilnya dalam total. (Seharusnya 55.)
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Use a while loop to print the numbers 5, 4, 3, 2, 1, each on its own line, then print Go. Start from n = 5. · Gunakan perulangan while untuk mencetak angka 5, 4, 3, 2, 1, masing-masing pada baris sendiri, lalu cetak Go. Mulai dari n = 5.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
nums is [3, 6, 7, 8, 10, 11]. Loop over it with a for-each loop and count how many values are even (use % 2 == 0). Store the count in evens. (It should be 3.) · nums adalah [3, 6, 7, 8, 10, 11]. Loop melalui item dengan for-each loop dan hitung berapa banyak nilai yang genap (gunakan % 2 == 0). Simpan jumlah tersebut dalam evens. (Seharusnya 3.)
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.