Iteration: FOR · Iterasi: FOR
FOR repeats a fixed number of times
- A
FORloop counts from a start value to an end value. NEXTmarks the end of the loop body.
FOR mengulang sejumlah kali tetap
- Perulangan
FORmenghitung dari nilai awal hingga nilai akhir. NEXTmenandai akhir tubuh perulangan.
FOR I ← 1 TO 5
OUTPUT I // 1, 2, 3, 4, 5
NEXT I
A running total
- Start a total at 0, then add to it inside the loop.
Total berjalan
- Mulai total di 0, lalu tambahkan ke dalamnya di dalam perulangan.
Total ← 0
FOR I ← 1 TO 10
Total ← Total + I
NEXT I
OUTPUT Total // → 55
Trace the running total · Jejak total berjalan
A FOR loop repeats a set number of times. Step through and watch Total grow. · Perulangan FOR mengulang sejumlah jumlah tertentu. Ikuti langkah demi langkah dan amati Total bertambah.
STEP changes the count
STEPsets how much the counter changes each pass.STEP -1counts down.
STEP mengubah hitungan
STEPmenetapkan berapa banyak counter berubah setiap kali larian.STEP -1menghitung mundur.
FOR I ← 5 TO 1 STEP -1
OUTPUT I // 5, 4, 3, 2, 1
NEXT I
Watch out
- Use
FORwhen you know how many times to repeat; useWHILEwhen you do not. - The counter (
I) andNEXT Imust use the same name.
Hati-hati
- Gunakan
FORketika Anda tahu berapa kali mengulang; gunakanWHILEketika tidak. - Counter (
I) danNEXT Iharus menggunakan nama yang sama.
Common mistakes
FOR I ← 1 TO N ... NEXT Irunsntimes, including both ends.- The counter changes automatically each turn.
Kesalahan umum
FOR I ← 1 TO N ... NEXT Iberjalannkali, termasuk kedua ujungnya.- Counter berubah otomatis setiap giliran.
Now you try
- Use
FOR ... NEXT. Type<-for←. - Press Check answer to test it.
Sekarang Anda coba
- Gunakan
FOR ... NEXT. Ketik<-untuk←. - Tekan Cek jawaban untuk mengujinya.
Use a FOR loop to output the numbers 1 to 5, one per line. · Gunakan perulangan FOR untuk outputkan angka 1 hingga 5, satu per baris.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Use a FOR loop to add up 1 to 10, then output the total (it is 55). · Gunakan perulangan FOR untuk menjumlahkan 1 hingga 10, lalu tampilkan totalnya (nilainya adalah 55).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Read an INTEGER N, then output N × 1, N × 2, N × 3 (one per line). For input 4, output 4, 8, 12. · Baca bilangan bulat INTEGER N, lalu tampilkan N × 1, N × 2, N × 3 (satu per baris). Untuk input 4, tampilkan 4, 8, 12.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.