1 / 33

Boolean

Boolean. Topik. Boolean Type int sbg Boolean Boolean expressions Boolean Operators Precedence (keutamaan) Kesalahan umum. Boolean. “type” khusus yg bernilai hanya: false dan true . Digunakan utk kondisi seleksi dan loop dalam algoritma. Type int sbg Boolean.

takara
Télécharger la présentation

Boolean

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Boolean

  2. Topik • Boolean • Type int sbg Boolean • Boolean expressions • Boolean Operators • Precedence (keutamaan) • Kesalahan umum

  3. Boolean • “type” khusus yg bernilai hanya: false dan true. • Digunakan utk kondisi seleksi dan loop dalam algoritma

  4. Type int sbg Boolean • Dlm C, integers di pakai sbg Boolean • Nilai integer 0 adalah false. • Dan integer non-zero adalah true.

  5. contoh: Outputnya apa? #include <stdio.h> /* Test Boolean. */ int main() { int whatever = 0; if (whatever) { printf(“Apakan ini “true”,Wied?\n”); } else { printf(“Tidak, Marge.\n”); } return 0; }

  6. contoh: Outputnya apa? #include <stdio.h> /* Test some Booleans. */ int main() { int whatever = 1; if (whatever) { printf(“Apakanini “true”,Wied?\n”); } else { printf(“No, Marge.\n”); } return 0; }

  7. contoh: Outputnya apa? #include <stdio.h> /* Test some Booleans. */ int main() { int whatever = -100; if (whatever) { printf(“Apakanini “true”,Wied?\n”); } else { printf(“No, Marge.\n”); } return 0; }

  8. contoh: Outputnya apa? #include <stdio.h> /* Test some Booleans. */ int main() { int whatever = ’A’; if (whatever) { printf(“Apakanini “true”,Wied?\n”); } else { printf(“No, Marge.\n”); } return 0; }

  9. contoh: Outputnya apa? #include <stdio.h> /* Test some Booleans. */ int main() { int whatever = 0.003; if (whatever) { printf(“Apakanini “true”,Wied?\n”); } else { printf(“No, Marge.\n”); } return 0; }

  10. contoh: Outputnya apa? hasilnya “undefined.” #include <stdio.h> /* Test some Booleans. */ int main() { float whatever = 0.003; if (whatever) { printf(“Apakanini “true”,Wied?\n”); } else { printf(“No, Marge.\n”); } return 0; }

  11. Boolean Expressions • ...ekspresi yg dpt di evaluasi dg tegas true atau false. • Boolean expression mengevaluasi true jika nilai integer 1; lainnya, 0.

  12. Boolean Operators • ...dipakai membentuk ekspresi boolean. • ...dikenal sbg operator “logical” • And (&&) • Or (||) • Not (!) • Equality (==) • Inequality (!=) • Comparison (<, >, <=, >=)

  13. And • True jika dan hanya jika kedua argumennya adalah true. Contoh: 1 && 2 1 && 0 && -1 tall && dark && handsome

  14. And • True jika dan hanya jika kedua argumennya adalah true. Contoh: 1 && 2 1 && 0 && -1 tall && dark && handsome Beda dg “bitwise AND” (&)

  15. Or • True jika hanya salah satu argumennya true (atau keduanya true). Examples: 1 || 2 11 || 0 || 1 good || bad || ugly

  16. Or • True jika hanya salah satu argumennya true (atau keduanya true). Beda dg “bitwise OR” (|) contoh: 1 || 2 11|| 0 || 1 good || bad || ugly

  17. Not • True jika argumen Boolean adalah false. contoh: ! 1 ! 0 ! happy

  18. Equality • True jika kedua argumennya bernilai sama contoh: 1 == 2 1 == 0 42 == 42 truth == beauty

  19. Equality • True jika kedua argumennya bernilai sama contoh: 1 == 2 1 == 0 42 == 42 truth == beauty Beda dg assignment (=)

  20. Inequality • True jika argumennya berbeda nilai. contoh: 1 != 2 1 != 0 42 != 42 truth != beauty

  21. Comparison • True jika memenuhi suatu relasi contoh: 1 < 2 0 > 1 42 <= 42 age >= 18

  22. Comparison • True jika memenuhi suatu relasi contoh: 1 < 2 0 > 1 42 <= 42 age>=18 Hati-hati!

  23. Keutamaan • Urutan keutamaan dr Highest ke lowest: • Bracket (kurung) • Not (!) • Comparison (<, >, <=, >=) • Equality (==) • Inequality (!=) • And (&&) • Or (||) Note: operator assignment (=) adalah urutan terendah yg diutamakan dari pada operator Boolean / Logical.

  24. contoh: #include <stdio.h> int main() { int age = 18; int haveMoney = 0; int haveCard = 1; float thirst = 0.31; int afterHours = 1; int result; result = age >= 18 && (haveMoney || haveCard) && thirst > 0.3 && ! afterHours; printf("%d\n", result); return 0; } bool.c

  25. Kesalahan Umum • Penggunaan = di banding ==

  26. contoh: #include <stdio.h> /* Kesalahan umum pd pemrograman c. */ int main() { int score; scanf("%d", &score); if (score = 48 || score = 49) { printf("Almost!\n"); } return 0; } boolerr1.c

  27. Example: #include <stdio.h> /* Kesalahanumum pd pemrograman c. */ int main() { int score; scanf("%d", &score); if (score = 48 || score = 49) { printf("Almost!\n"); } return 0; } Biasanya pesan error (jika ada): “LValue required...” boolerr1.c

  28. Example: #include <stdio.h> /* Kesalahanumum pd pemrograman c. */ int main() { int score; scanf("%d", &score); if (score == 48 || score == 49) { printf("Almost!\n"); } return 0; } boolerr1.c

  29. Kesalahan Umum • Penggunaan = drpd == • Pada perbandingan yang banyak

  30. contoh: #include <stdio.h> /* Another common C error. */ int main() { int score; scanf("%d", &score); if ( 0 < score < 48 ) { printf("Fail\n"); } return 0; } boolerr2.c

  31. contoh: #include <stdio.h> /* Another common C error. */ int main() { int score; scanf("%d", &score); if ( 0 < score < 48 ) { printf("Fail\n"); } return 0; } 0 atau 1 boolerr2.c

  32. contoh: #include <stdio.h> /* Another common C error. */ int main() { int score; scanf("%d", &score); if ( 0 < score < 48 ) { printf("Fail\n"); } return 0; } Selalu 1 0 or 1 boolerr2.c

  33. contoh: #include <stdio.h> /* Another common C error. */ int main() { int score; scanf("%d", &score); if ( 0 < score && score < 48 ) { printf("Fail\n"); } return 0; } boolerr2.c

More Related