Nested loops & 2-D lists · Perulangan bersarang & daftar 2-D
A loop inside a loop
- You can put one loop inside another loop.
- For every pass of the outer loop, the inner loop runs fully.
- So an outer of 2 and an inner of 3 runs the body 2 × 3 = 6 times.
Loop di dalam loop
- Anda dapat menempatkan satu loop di dalam loop lain.
- Untuk setiap putaran loop luar, loop dalam akan berjalan secara penuh.
- Jadi, loop luar sebanyak 2 dan loop dalam sebanyak 3 akan menjalankan tubuh kode 2 × 3 = 6 kali.
for row in range(2):
for col in range(3):
print(row, col)
A list of lists
- A 2-D list is a list whose items are themselves lists.
- It models a grid or table: rows of columns.
- Reach one cell with two indexes:
grid[row][col].
Daftar berisi daftar
- Daftar 2-D adalah daftar yang elemennya sendiri-sendiri juga berupa daftar.
- Ia memodelkan grid atau tabel: baris dari kolom-kolom.
- Akses satu sel menggunakan dua indeks:
grid[row][col].
grid = [[1, 2], [3, 4]]
print(grid[0][1])
print(grid[1][0])
Loop over a grid
- Use a nested loop to visit every cell.
- The outer loop walks the rows; the inner loop walks the columns.
for row in grid:gives one row (a list) at a time.
Loop melalui grid
- Gunakan loop bersarang untuk mengunjungi setiap sel.
- Loop luar menelusuri baris; loop dalam menelusuri kolom.
for row in grid:memberikan satu baris (sebuah daftar) sekaligus.
grid = [[1, 2], [3, 4]]
for row in grid:
for value in row:
print(value)
In Cambridge pseudocode
- A 2-D list is the exam's 2-D array, written
ARRAY[1:rows, 1:cols].
Dalam pseudocode Cambridge
- Daftar 2-D adalah array 2-D dalam ujian, ditulis
ARRAY[1:rows, 1:cols].
DECLARE grid : ARRAY[1:2, 1:3] OF INTEGER
FOR r ← 1 TO 2
FOR c ← 1 TO 3
OUTPUT grid[r, c]
NEXT c
NEXT r
Common mistakes
- The inner loop finishes all its turns for each single turn of the outer loop.
- Index a grid row first, then column:
grid[row][col].
Kesalahan umum
- Loop dalam menyelesaikan seluruh perulangannya untuk setiap satu putaran loop luar.
- Indeks baris grid terlebih dahulu, lalu kolom:
grid[row][col].
Now you try
- Use a nested loop in each task.
- Press Check answer to test your code.
Sekarang Anda coba
- Gunakan loop bersarang di setiap tugas.
- Tekan Periksa jawaban untuk menguji kode Anda.
A 2-D list is a grid · Daftar 2-D adalah grid
A 2-D list is rows of columns; reach a cell with grid[row][col]. · Daftar 2-D adalah baris dari kolom; akses sel dengan grid[row][col].
Use a nested loop to print a 3-by-3 square of stars: three lines, each with three * characters. · Gunakan perulangan nested untuk mencetak kotak bintang 3x3: tiga baris, masing-masing dengan tiga karakter *.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Add up every number in the 2-D list grid with a nested loop. Store the total in total (the answer is 21). · Jumlahkan semua angka dalam daftar 2-D grid menggunakan perulangan nested. Simpan totalnya di total (jawabannya adalah 21).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Count how many cells in the 2-D list grid are equal to 0. Store the count in count (the answer is 3). · Hitung berapa banyak sel dalam daftar 2-D grid yang sama dengan 0. Simpan jumlahnya di count (jawabannya adalah 3).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.