380 likes | 458 Views
Selection (Seleksi). Topik. Statemen (pernyataan) if Statemen (pernyataan) else Cascaded if Nested if. Baca buku mengenai c. Statement if. Menentukan block yg akan di eksekusi. Di implementasikan pd selection instructions dlm algorithm.
E N D
Topik • Statemen (pernyataan) if • Statemen (pernyataan) else • Cascadedif • Nestedif Baca buku mengenai c
Statement if • Menentukan block yg akan di eksekusi. • Di implementasikan pd selection instructions dlm algorithm. • Memutuskan utk melakukan evaluasi suatu Boolean expression. • Jika ekspresi true (non-zero), block dieksekusi.
contoh: oddnum.c Baca suatu angka, dan cetak jika ganjil. output “Masukan suatu integer” input number if (number adlh ganjil ) then { output number }
contoh : oddnum.c #include <stdio.h> /* Baca suatuangka, dancetakjikaganjil.*/ int main() { return 0; } Baca suatuangka, dancetakjikaganjil. output “Masukansuatu integer” input number if (number adlhganjil ) then { output number }
contoh : oddnum.c #include <stdio.h> /* Baca suatuangka, dancetakjikaganjil.*/ int main() { int number; printf(“Masukansuatu integer: "); scanf("%d", &number); return 0; } Baca suatuangka, dancetakjikaganjil. output “Masukansuatu integer” input number if (number adlhganjil ) then { output number }
contoh : oddnum.c #include <stdio.h> /* Baca suatuangka, dancetakjikaganjil.*/ int main() { int number; printf(“Masukansuatu integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d\n", number); } return 0; } Baca suatuangka, dancetakjikaganjil. output “Masukansuatu integer” input number if (number adlhganjil ) then { output number }
contoh : oddnum.c #include <stdio.h> /* Baca suatuangka, dancetakjikaganjil.*/ int main() { int number; printf(“Masukansuatu integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d\n", number); } return 0; } Baca suatuangka, dancetakjikaganjil. output “Masukansuatu integer” input number if (number adlhganjil ) then { output number } Jgn letakan “then” disini!
contoh : oddnum.c #include <stdio.h> /* Baca suatuangka, dancetakjikaganjil.*/ int main() { int number; printf(“Masukansuatu integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d\n", number); } return 0; } Baca suatuangka, dancetakjikaganjil. output “Masukansuatu integer” input number if (number adlhganjil ) then { output number } Jgn letakan semicolon disini!
contoh : oddnum.c #include <stdio.h> /* Baca suatuangka, dancetakjikaganjil.*/ int main() { int number; printf(“Masukansuatu integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d\n", number); } return 0; } Baca suatu angka, dan cetak jika ganjil. output “Masukan suatu integer” input number if (number adlh ganjil ) then { output number }
Catatan pada if A if (number % 2 != 0) { printf("%d", number); } printf(” is odd\n"); • Potongan kode mana yang sama ? B if (number % 2 != 0) printf("%d", number); printf(” is odd\n"); C if (number % 2 != 0) { printf("%d", number); printf(” is odd\n"); }
Catatan pada if A if (number % 2 != 0) { printf("%d", number); } printf(” is odd\n"); • Potongan kode mana yang sama ? B if (number % 2 != 0) printf("%d", number); printf(” is odd\n"); C if (number % 2 != 0) { printf("%d", number); printf(” is odd\n"); }
Catatan pada if • Kesalahan umum if (number % 2 != 0); { printf("%d adlh ganjil “,number); } printf("number\n");
Catatan pada if • Kesalahan umum if (number % 2 != 0); { printf("%d is an odd ", number); } printf("number\n"); No semi-colon disini!
Catatan pada if • Kesalahan umum if (number = 0) { printf("%d\n", number); } printf("%d\n", number);
Catatan pada if • Kesalahan umum if (number = 0) { printf("%d\n", number); } printf("%d\n", number); Should be ==
Staement else • Muncul setelah statement if • Dieksekusi hanya jika blok if tidak di eksekusi
contoh: oddeven.c #include <stdio.h> /* Baca suatuangka, dancetakjikaganjil. */ main() { int number; printf(“Masukansuatu integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d adalahbilanganganjil\n", number); } } Baca suatu angka, dan cetak jika ganjil. output “Masukan suatu integer” input number if (number is odd) then { output: number “ adlh bilangan ganjil ” } else { output: number “ adlh bilangan genap” }
contoh : oddeven.c #include <stdio.h> /* Baca suatuangka, dancetakjikaganjil. */ main() { int number; printf(“Masukansuatu integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d adalahbilanganganjil\n", number); } else { printf("%d adalahbilangangenap\n", number); } } Baca suatuangka, dancetakjikaganjil. output “Masukansuatu integer” input number if (number is odd) then { output: number “ adlhbilanganganjil ” } else { output: number “ adlhbilangangenap” }
contoh : oddeven.c #include <stdio.h> /* Baca suatuangka, dancetakjikaganjil. */ main() { int number; printf(“Masukansuatu integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d adalahbilanganganjil\n", number); } else { printf("%d adalahbilangangenap\n", number); } } Baca suatuangka, dancetakjikaganjil. output “Masukansuatu integer” input number if (number is odd) then { output: number “ adlhbilanganganjil ” } else { output: number “ adlhbilangangenap” } No semicolons disini!
contoh : oddeven.c #include <stdio.h> /* Baca suatuangka, dancetakjikaganjil. */ main() { int number; printf(“Masukansuatu integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d adalahbilanganganjil\n", number); } else { printf("%d adalahbilangangenap\n", number); } } Baca suatuangka, dancetakjikaganjil. output “Masukansuatu integer” input number if (number is odd) then { output: number “ adlhbilanganganjil ” } else { output: number “ adlhbilangangenap” }
Statement Cascaded if • Blok Alternatif banyak dengan ekspresi Boolean. • Ekspresi pertama di evaluasi ke true, yg menyebabkan eksekusi pada blok yang di asosiasikan. (ada default blok) • Hanya satu blok yang akan di eksekusi.
contoh: months.c output “Enter an integer” input month if (month is September, or April, or June, or November) then { output “30 days” } else if (month is February) { output “28 or 29 days” } else { output “31 days” } Menentukan jumlah hari dalam sebulan : 30 adalah September, April, June dan November. Sisanya 31, kecuali February , ada 28 dan 29 pd thn kabisat (leap year).
Example: months.c int main() { return 0; } • #include <stdio.h> • /*************************\ • Menentukanjumlahharidalamsebulan : • 30 adalah September, • April, June dan November. • Sisanya 31, • kecuali February , • ada 28 dan 29 pd thnkabisat (leap year). • \*************************/ • const int September = 9; • const int April = 4; • const int June = 6; • const int November = 11; • const int February = 2;
Example: months.c int main() { int month; printf(“Masukan jmlh hari dlm sebulan: "); scanf("%d", &month); return 0; } • #include <stdio.h> • /*************************\ • Menentukanjumlahharidalamsebulan : • 30 adalah September, • April, June dan November. • Sisanya 31, • kecuali February , • ada 28 dan 29 pd thnkabisat (leap year). • \*************************/ • const int September = 9; • const int April = 4; • const int June = 6; • const int November = 11; • const int February = 2;
Example: months.c int main() { int month; printf("Masukanjmlhharidlmsebulan:"); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 hai\n"); } return 0; } • #include <stdio.h> • /*************************\ • Menentukanjumlahharidalamsebulan : • 30 adalah September, • April, June dan November. • Sisanya 31, • kecuali February , • ada 28 dan 29 pd thnkabisat (leap year). • \*************************/ • const int September = 9; • const int April = 4; • const int June = 6; • const int November = 11; • const int February = 2;
Example: months.c int main() { int month; printf("Masukanjmlhharidlmsebulan: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 hari\n"); } return 0; } • #include <stdio.h> • /*************************\ • Menentukanjumlahharidalamsebulan : • 30 adalah September, • April, June dan November. • Sisanya 31, • kecuali February , • ada 28 dan 29 pd thnkabisat (leap year). • \*************************/ • const int September = 9; • const int April = 4; • const int June = 6; • const int November = 11; • const int February = 2; Kesalahan umum: if (month==September || April || June || November )
Example: months.c int main() { int month; printf("Masukanjmlhharidlmsebulan: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 hari\n"); } else if (month==February) { printf("28 atau 29 hari\n"); } return 0; } • #include <stdio.h> • /*************************\ • Menentukanjumlahharidalamsebulan : • 30 adalah September, • April, June dan November. • Sisanya 31, • kecuali February , • ada 28 dan 29 pd thnkabisat (leap year). • \*************************/ • const int September = 9; • const int April = 4; • const int June = 6; • const int November = 11; • const int February = 2;
Example: months.c int main() { int month; printf("Masukanjmlhharidlmsebulan: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 hari\n"); } else if (month==February) { printf("28 atau 29 hari\n"); } else { printf("31 hari\n"); } return 0; } • #include <stdio.h> • /*************************\ • Menentukanjumlahharidalamsebulan : • 30 adalah September, • April, June dan November. • Sisanya 31, • kecuali February , • ada 28 dan 29 pd thnkabisat (leap year). • \*************************/ • const int September = 9; • const int April = 4; • const int June = 6; • const int November = 11; • const int February = 2;
Example: months.c int main() { int month; printf("Masukanjmlhharidlmsebulan: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 hari\n"); } else if (month==February) { printf("28 atau 29 hari\n"); } else { printf("31 hari\n"); } return 0; } • #include <stdio.h> • /*************************\ • Menentukanjumlahharidalamsebulan : • 30 adalah September, • April, June dan November. • Sisanya 31, • kecuali February , • ada 28 dan 29 pd thnkabisat (leap year). • each leap year. • \*************************/ • const int September = 9; • const int April = 4; • const int June = 6; • const int November = 11; • const int February = 2; “Default” block.
Example: months.c int main() { int month; printf(“Masukanjmlhharidlmsebulan:"); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 hari\n"); } else if (month==February) { printf("28 atau 29 hari\n"); } else { printf("31 hari\n"); } return 0; } • #include <stdio.h> • /*************************\ • Menentukanjumlahharidalamsebulan : • 30 adalah September, • April, June dan November. • Sisanya 31, • kecuali February , • ada 28 dan 29 pd thnkabisat (leap year). • \*************************/ • const int September = 9; • const int April = 4; • const int June = 6; • const int November = 11; • const int February = 2;
Catatan pada Cascaded if if (letter >= ’a’) { printf(“S1\n”); } else if (letter <= ’z’) { printf(“S2\n”); } else if (letter >= ’A’) { printf(“S3\n”); } else if (letter <= ’Z’) { printf(“S4\n”); } Q: • Apa output-nya jika: • huruf sama dengan ‘b’ • huruf sama dengan‘z’ • huruf sama dengan‘A’ • huruf sama dengan‘X’
Contoh lain if (ch >= ’a’ && ch <= ’z’) { printf(“%c is in lower case.\n”, ch); } else if (ch >= ’A’ && ch <= ’Z’) { printf(“%c is in upper case.\n”. ch); } else if (ch >= ’0’ && ch <= ’9’) { printf(“%c is a digit with value %d.\n”, ch, ch - ’0’); }
More Examples if (ch >= ’a’ && ch <= ’z’) { printf(“%c is in lower case.\n”, ch); } else if (ch >= ’A’ && ch <= ’Z’) { printf(“%c is in upper case.\n”. ch); } else if (ch >= ’0’ && ch <= ’9’) { printf(“%c is a digit with value %d.\n”, ch, ch - ’0’); }
Contoh: Nested if if ( coldWeather ) { wearJumper = 1; wearRaincoat = wearJacket = wearThermal = 0; if ( raining ) wearRaincoat = 1; else wearJacket = 1; if ( belowZero ) { wearThermal = 1; } }
contoh: Nested if if ( coldWeather ) { wearJumper = 1; wearRaincoat = wearJacket = wearThermal = 0; if ( raining ) wearRaincoat = 1; else wearJacket = 1; if ( belowZero ) { wearThermal = 1; } }
contoh: Nested if if ( coldWeather ) { wearJumper = 1; wearRaincoat = wearJacket = wearThermal = 0; if ( raining ) wearRaincoat = 1; else wearJacket = 1; if ( belowZero ) { wearThermal = 1; } }
contoh: Nested if if ( coldWeather ) { wearJumper = 1; wearRaincoat = wearJacket = wearThermal = 0; if ( raining ) wearRaincoat = 1; else wearJacket = 1; if ( belowZero ) { wearThermal = 1; } }