1 / 31

PEMROGRAMAN BERORIENTASI OBJEK

PEMROGRAMAN BERORIENTASI OBJEK. Kondisional Perulangan Percabangan. Struktur Kontrol. 1. Kondisional. Pernyataan if Pernyataan if – else Pernyataan if – else if Pernyataan switch. 2. Kondisional.

cate
Download Presentation

PEMROGRAMAN BERORIENTASI OBJEK

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. PEMROGRAMAN BERORIENTASI OBJEK • Kondisional • Perulangan • Percabangan StrukturKontrol

  2. 1. Kondisional • Pernyataan if • Pernyataan if – else • Pernyataan if – else if • Pernyataan switch

  3. 2. Kondisional Pernyataan kondisional memanfaatkan ekspresi boolean yang dapatberupa true atau false (sehinggadisebutbinary decision). Aksi yang dikerjakantergantungpadanilaihasildariekspresi:

  4. Pernyataan if • Pernyataan if menentukansebuah statement yang akandieksekusijikadanhanyajikapersyaratanboolean (boolean statement) bernilai true. Bentukumum : if( boolean_expression ) statement; Atau if( boolean_expression ){ statement1; statement2; . . . }

  5. Flowchart Pernyataan if

  6. Pernyataan if - else Pernyataan if-else digunakanapabilakitainginmengeksekusisebuah statement dengankondisitrue dan statement yang lain dengankondisi false. Bentukumum : if( boolean_expression ) atau if(boolean_expression) statement; { else statement1; statement; statement2; } else{ statement; … ; }

  7. Flowchart Pernyataan if-else

  8. Contoh : public class coba { public static void main(String[]args) { int grade = 68; if( grade > 60 ){ System.out.println(“selamat!"); System.out.println(“kamu lulus!"); } else{ System.out.println(“maaf, kamutidak lulus"); } } }

  9. Pernyataan if-else if Bentukumum : if( boolean_expression1 ) statement1; else if( boolean_expression2 ) statement2; else statement3;

  10. Flowchart pernyataan if-else if

  11. Pernyataan switch switch( switch_expression ){ case case_selector1: statement1; // statement2; //block 1 . . . // break; case case_selector2: statement1; // statement2; //block 2 . . . // break; . . . default: statement1; // statement2; //block n . . . // break; }

  12. Flowchart switch

  13. Contoh : public class coba { public static void main(String[]args) { int grade = 80; switch(grade){ case 100: System.out.println( "Excellent!" ); break; case 90: System.out.println("Good job!" ); break; case 80: System.out.println("Study harder!" ); break; default: System.out.println("Sorry, you failed."); }}}

  14. 2. Perulangan • Pernyataan while • Pernyataan do..while • Pernyataan for

  15. Pernyataan while • Pernyataan while akandijalankansecaraterus-menerusselamakondisibernilaibenar(true). • Bentukumum : while( boolean_expression ) { statement1; statement2; . . . }

  16. Contoh : public class coba { public static void main(String[]args) { inti = 4; while ( i > 0 ){ System.out.print(i); i--; } } }

  17. Pernyataan do..while • Pernyataan do..while, statement dieksekusisetidaknyasatu kali. • Bentukumum : do{ statement1; statement2; . . . }while( boolean_expression );

  18. Contoh : public class coba { public static void main(String[]args) { int x = 0; do { System.out.print(x); x++; }while (x>10); } }

  19. Pernyataan for • Pernyataan for , melakukaneksekusipengulanganbeberapa kali. • Bentukumum : for (Initialization; LoopCondition; StepExpression){ statement1; statement2; . . . } Initialization – inisialisasi dari variabel loop. LoopCondition - membandingkanvariabel loop padanilaibatas. StepExpression - melakukan update pada variabel loop.

  20. Contoh : public class coba { public static void main(String[]args) { inti; for( i = 0; i < 10; i++ ){ System.out.print(i); } } }

  21. Percabangan • Pernyataan break • Pernyataan continue

  22. Pernyataan break • Pernyataan break digunakan utk menghentikan jalannya statement. • Pernyataan break ada 2 : - unlabeled break - labeled break

  23. Unlabeled break while(…){ while (…) { if (…) break; … } //akhir while terdalam pernyataansesudah while …. } //akhir while terluar

  24. Contoh : public class cabang { public static void main(String[]args) { int i=0, j=0; while (i<3){ j=0; while(j<5){ if (j==3) break; System.out.println("i = " + i +" j = "+ j); j++; } i++; } }}

  25. Hasil

  26. Labeled break selesai : while(…){ while (…) { if (…) break selesai; … } //akhir while terdalam pernyataansesudah while …. } //akhir while terluar Pernyataan_x; Label

  27. Contoh System.out.println("i = " + i +" j = "+ j); j++; } i++; } System.out.println("Akhir program"); } } public class cabang { public static void main(String[]args) { int i=0, j=0; selesai: while (i<3){ j=0; while(j<5){ if (j==3) break selesai;

  28. Contoh :

  29. Pernyataan continue • Pernyataancontunieberfungsiutkmengarahkaneksekusikekondisipernyataanperulanganshgkondisiakandievaluasilagi.

  30. Contoh : public class cabang { public static void main(String[]args) { inti=0; while(i<5){ if (i==3){ i++; continue; } System.out.println(i); i++; } } }

  31. Tugas • Buat flowchart dan program utk- kondisional (if, if-else, switch) - perulangan (while, do.. while, for) - percabangan(break, continue)

More Related