Booleans and selection: if, else, switch · Boolean dan seleksi: if, else, switch
True and false in C
- C has no separate true/false type by default. It uses numbers:
0means false, and any other number means true. - A test like
5 > 3produces1(true).5 < 3produces0(false). - (If you
#include <stdbool.h>you can writebool,true, andfalse— but plainintworks everywhere.)
True dan false di C
- C tidak memiliki tipe true/false terpisah secara default. Ia menggunakan angka:
0berarti false, dan angka lain apa pun berarti true. - Test seperti
5 > 3menghasilkan1(true).5 < 3menghasilkan0(false). - (Jika Anda
#include <stdbool.h>Anda bisa menulisbool,true, danfalse— tetapiintbiasa berfungsi di mana saja.)
Comparing and combining
- Compare values with
==(equal),!=(not equal),<,>,<=,>=. - Be careful:
==tests equality; a single=assigns a value. They are different! - Combine tests with
&&(and),||(or), and!(not).a && bis true only when both are true.
Membandingkan dan menggabungkan
- Bandingkan nilai dengan
==(sama dengan),!=(tidak sama dengan),<,>,<=,>=. - Hati-hati:
==menguji kesetaraan; satu=mengassign nilai. Mereka berbeda! - Gabungkan test dengan
&&(dan),||(atau), dan!(tidak).a && btrue hanya ketika keduanya true.
if / else if / else
if (test) { ... }runs the block only when the test is true.- Add
else if (...)to check another test, andelsefor "none of the above". - Only the first matching branch runs.
if / else if / else
if (test) { ... }menjalankan blok hanya ketika test true.- Tambahkan
else if (...)untuk memeriksa test lain, danelseuntuk "tidak ada yang di atas". - Hanya cabang pertama yang cocok yang berjalan.
#include <stdio.h>
int main(void) {
int score = 72;
if (score >= 90) {
printf("A\n");
} else if (score >= 60) {
printf("Pass\n"); // this one runs
} else {
printf("Fail\n");
}
return 0;
}
switch: choosing among many values
- A
switchpicks a branch based on one value (anintorchar). - Each
caseends withbreak;so it does not fall into the next one.default:catches everything else. switchis neat when you compare the same variable against many fixed values.
switch: memilih di antara banyak nilai
- Sebuah
switchmemilih cabang berdasarkan satu nilai (intatauchar). - Setiap
casediakhiri denganbreak;sehingga tidak jatuh ke yang berikutnya.default:menangkap semua lainnya. switchrapi ketika Anda membandingkan variabel yang sama terhadap banyak nilai tetap.
Common mistakes
- Use
==to compare;=assigns (andif (x = 5)is always true). - Each
switchcase needs abreak;.
Kesalahan umum
- Gunakan
==untuk membandingkan;=mengassign (danif (x = 5)selalu true). - Setiap kasus
switchmembutuhkanbreak;.
Now you try
- Use comparisons and
&&/||to build your tests. Remember0is false, anything else is true. - For the
switchtask, divide the score by 10 first, then switch on the result. - Do not write a
main— the checker provides one.
Sekarang Anda coba
- Gunakan perbandingan dan
&&/||untuk membangun test Anda. Ingat0adalah false, segalanya else true. - Untuk tugas
switch, bagi skor dengan 10 terlebih dahulu, lalu switch pada hasilnya. - Jangan tulis
main— checker menyediakannya.
if / else if / else
Conditions test top-down; the first true block runs. · Kondisi diuji dari atas ke bawah; blok pertama yang benar akan dijalankan.
Complete int sign(int n) so it returns -1 when n is negative, 0 when n is zero, and 1 when n is positive. Do not write a main. · Lengkapi int sign(int n) agar mengembalikan -1 ketika n negatif, 0 ketika n nol, dan 1 ketika n positif. Jangan tulis main.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Complete int in_range(int n, int lo, int hi) so it returns 1 when n is between lo and hi (inclusive), and 0 otherwise. Use &&. Do not write a main. · Lengkapi int in_range(int n, int lo, int hi) agar mengembalikan 1 ketika n berada di antara lo dan hi (termasuk), dan 0 jika tidak. Gunakan &&. Jangan tulis main.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Complete char grade_letter(int score) using a switch on score / 10. Return 'A' for 90–100, 'B' for the 80s, 'C' for the 70s, 'D' for the 60s, and 'F' below 60. Do not write a main. · Lengkapi char grade_letter(int score) menggunakan switch pada score / 10. Kembalikan 'A' untuk skor 90–100, 'B' untuk rentang 80-an, 'C' untuk rentang 70-an, 'D' untuk rentang 60-an, dan 'F' untuk di bawah 60. Jangan tulis main.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.