Loops: while, for, and accumulation · Perulangan: while, for, dan akumulasi
Repeating work
- A loop runs the same block of code many times, so you don't copy it by hand.
- C has three loops:
while,for, anddo-while. They all repeat while a condition is true. - The condition is checked each time around. When it becomes false, the loop stops.
Mengulang pekerjaan
- Sebuah loop menjalankan blok kode yang sama berkali-kali, sehingga Anda tidak perlu menyalinnya secara manual.
- C memiliki tiga loop:
while,for, dando-while. Semua mengulang selama condition true. - Condition dicek setiap kali putaran. Ketika menjadi false, loop berhenti.
while loops
while (test) { ... }checks the test first, then runs the block if it is true.- Something inside the block must eventually make the test false, or the loop never ends.
- Use
whilewhen you don't know in advance how many times you will repeat.
while loops
while (test) { ... }memeriksa test terlebih dahulu, lalu menjalankan blok jika true.- Sesuatu di dalam blok harus akhirnya membuat test false, atau loop tidak pernah berakhir.
- Gunakan
whileketika Anda tidak tahu sebelumnya berapa kali Anda akan mengulang.
for loops
- A
forloop puts three parts in one line:for (start; test; step). for (int i = 0; i < 5; i++)means: startiat0, keep going whilei < 5, add1each time.- Use
forwhen you are counting a known number of times.
for loops
- Loop
formenempatkan tiga bagian dalam satu baris:for (start; test; step). for (int i = 0; i < 5; i++)berarti: mulaiidi0, terus selamai < 5, tambahkan1setiap kali.- Gunakan
forketika Anda menghitung jumlah waktu yang diketahui.
#include <stdio.h>
int main(void) {
int total = 0;
for (int i = 1; i <= 4; i++) {
total = total + i; // accumulate: 1, then 3, then 6, then 10
}
printf("%d\n", total); // 10
return 0;
}
Accumulation and counting
- An accumulator is a variable that builds up a result across the loop, like
totalabove. - A counter adds
1each time something happens, to count matches. break;stops the loop early;continue;skips to the next round.
Akumulasi dan penghitungan
- Sebuah accumulator adalah variabel yang membangun hasil melintasi loop, seperti
totaldi atas. - Sebuah counter menambahkan
1setiap kali sesuatu terjadi, untuk menghitung kecocokan. break;menghentikan loop lebih awal;continue;melewatkan ke putaran berikutnya.
Common mistakes
- Do not put a
;right afterfor(...)— it makes an empty loop. - Change the condition so the loop can end.
Kesalahan umum
- Jangan taruh
;tepat setelahfor(...)— itu membuat loop kosong. - Ubah condition agar loop dapat berakhir.
Now you try
- Set up your accumulator or counter before the loop, update it inside, and use it after.
- For the function tasks, do not write a
main— the checker provides one.
Sekarang Anda coba
- Setup accumulator atau counter Anda sebelum loop, update di dalam, dan gunakan setelah.
- Untuk tugas fungsi, jangan menulis
main— checker menyediakannya.
Loops and accumulation · Perulangan dan akumulasi
A loop repeats; a running total builds up each pass. · Perulangan mengulang; total berjalan terakumulasi setiap kali iterasi.
In main, use a for loop to print the numbers 1 to 5, each on its own line. · Di dalam main, gunakan perulangan for untuk mencetak angka-angka dari 1 hingga 5, masing-masing pada baris tersendiri.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Complete int sum_to(int n) so it returns 1 + 2 + ... + n. Return 0 when n is 0 or negative. Do not write a main. · Lengkapi int sum_to(int n) agar mengembalikan 1 + 2 + ... + n. Kembalikan 0 ketika n merupakan 0 atau negatif. Jangan tulis main.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Complete int count_multiples(int n, int k) so it returns how many numbers from 1 to n are divisible by k. Use % inside a loop. Do not write a main. · Lengkapi int count_multiples(int n, int k) agar mengembalikan berapa banyak angka dari 1 hingga n yang habis dibagi k. Gunakan % di dalam perulangan. Jangan tulis main.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.