1 / 27

Algoritma & Pemrograman 1

Algoritma & Pemrograman 1. Stuktur Kontrol. Macam”:. struktur kontrol keputusan (if, else, switch) fungsi:  u/ memilih blok kode yang akan dieksekusi struktur kontrol pengulangan (while, do-while, for) Fungsi  u/ melakukan pengulangan pada blok kode yang akan dieksekusi

marcy
Télécharger la présentation

Algoritma & Pemrograman 1

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. Algoritma & Pemrograman 1 Stuktur Kontrol

  2. Macam”: • struktur kontrol keputusan (if, else, switch) • fungsi:u/ memilih blok kode yang akan dieksekusi • struktur kontrol pengulangan (while, do-while, for) • Fungsiu/ melakukan pengulangan pada blok kode yang akan dieksekusi • statement percabangan (break, continue, return) • Fungsiuntuk mengatur redirection dari program

  3. Struktur kontrol keputusan Statemen If (1) • menentukan sebuah statement yang akan dieksekusi jika dan hanya jika persyaratan boolean (boolean statement) bernilai T • Bentuk: if( boolean_expression ) statement; or if( boolean_expression ){ statement1; statement2; . . . } int grade = 68; if( grade > 60 ) System.out.println("Congratulations!");

  4. Struktur kontrol keputusan Statemen if-else (1) • Digunakan u/ mengeksekusi sebuah statement dengan kondisi Tdan statement yang lain dengan kondisi F • Bentuk: if( boolean_expression ) statement; else statement; OR if( boolean_expression ){ statement1; statement2; . . . … } else{ statement1; statement2; . . . … }

  5. Struktur kontrol keputusan Statemen if-else (2) int grade = 68; if( grade > 60 ) System.out.println("Congratulations!"); else System.out.println("Sorry you failed"); OR int grade = 68; if( grade > 60 ){ System.out.println("Congratulations!"); System.out.println("You passed!"); } else{ System.out.println("Sorry you failed"); }

  6. Struktur kontrol keputusan Statemen if-else-if (1) • untuk membuat seleksi persyaratan yang lebih kompleks • Bentuk: if( boolean_expression1 ) statement1; else if( boolean_expression2 ) statement2; else statement3;

  7. Struktur kontrol keputusan Statemen if-else-if (1) int grade = 68; if( grade > 90 ){ System.out.println("Very good!"); } else if( grade > 60 ){ System.out.println("Very good!"); } else{ System.out.println("Sorry you failed"); }

  8. Struktur kontrol keputusan Statemen switch (1) • u/ melakukan percabangan dengan persyaratan yang beragam • Bentuk: 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; }

  9. Struktur kontrol keputusan Statemen switch (2)

  10. Struktur kontrol keputusan Statemen switch (3) public class Grade { public static void main( String[] args ) { int grade = 92; 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."); } } }

  11. Struktur Kontrol Perulangan while loop • statement atau blok statement yang diulang” sampai mencapai kondisi yang cocok while( boolean_expression ){ statement1; statement2; . . . } • Contoh 1 int i = 4; while ( i > 0 ){ System.out.print(i); i--; } • Contoh 2 while(true) System.out.println(“hello”); • Contoh 3: • int x = 0; • while (x<10) • { • System.out.println(x); • x++; • } • Contoh 4: • //no loops • while (false) • System.out.println(“hello”);

  12. Struktur Kontrol Perulangan do-while loop • akan dieksekusi beberapa kali selama kondisi bernilai T do{ statement1; statement2; . . . }while( boolean_expression ); • c/ 1 do{ System.out.println(“hello”); } while (true); • c/ 2: • int x = 0; • do • { • System.out.println(x); • x++; • }while (x<10); • c/ 3: • do • System.out.println(“hello”); • while (false);

  13. Struktur Kontrol Perulangan for-loop • melakukan pengulangan eksekusi code beberapa kali for (InitializationExpression; LoopCondition; StepExpression){ statement1; statement2; . . . } InitializationExpressioninisialisasi dari variabel loop LoopConditionmembandingkan variabel loop d/ nilai batas StepExpressionmelakukan update pada variabel loop

  14. Struktur Kontrol Perulangan for-loop • c/ int i; for( i = 0; i < 10; i++ ){ System.out.print(i); } • = int i = 0; while( i < 10 ){ System.out.print(i); i++; }

  15. Branching Statements • Java mengijinkan user untuk mengatur jalannya eksekusi program • Bentuk: • breakunlabeled & labeled menghentikan jalannya statement switch,for, while atau do-while loop • continueunlabeled & labeled untuk melewati pengulangan dari for, while, atau do-while loop yang sedang berjalan • return

  16. Break(1)unlabeled String names[] = {"Beah", "Bianca", "Lance", "Belle", "Nico", "Yza", "Gem", "Ethan"}; String searchName = "Yza"; boolean foundName = false; for( int i=0; i< names.length; i++ ){ if( names[i].equals( searchName )){ foundName = true; break; } } if( foundName ){ System.out.println( searchName + " found!" ); } else{ System.out.println( searchName + " not found." ); }

  17. Break(2)labeled • akan menghentikan statement luardiidentifikasikan berupa label pada statement break

  18. Break(2)labeled Int[ ][ ] numbers = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int searchNum = 5; boolean foundNum = false; searchLabel: for( int i=0; i<numbers.length; i++ ){ for( int j=0; j<numbers[i].length; j++ ){ if( searchNum == numbers[i][j] ){ foundNum = true; break searchLabel; } } } if( foundNum ){ System.out.println( searchNum + " found!" ); } else{ System.out.println( searchNum + " not found!" ); }

  19. Continue(1) unlabeled • Akan melewati bagian pengulangan pada loop String names[] = {"Beah", "Bianca", "Lance", "Beah"}; int count = 0; for( int i=0; i<names.length; i++ ){ if( !names[i].equals("Beah") ){ continue; //skip next statement } count++; } System.out.println("There are " + count + " Beahs in the list");

  20. Continue(2) labeled • akan melanjutkan sebuah statement dengan melewati pengulangan yang sedang berjalan dari loop terluar yang diberi label (tanda). outerLoop: for( int i=0; i<5; i++ ){ for( int j=0; j<5; j++ ){ System.out.println("Inside for(j) loop"); //message1 if( j == 2 ) continue outerLoop; } System.out.println("Inside for(i) loop"); //message2 }

  21. return • untuk keluar dari sebuah fungsi (method) • Statement return memiliki dua bentuk d/: • sebuah nilai Untuk memberikan sebuah nilai, cukup berikan nilai (atau ekspresi yang menghasilkan sebuah nilai) sesudah return. Contohnya, return ++count; OR return "Hello"; • tidak memberikan nilaiKetika sebuah method void dideklariskan return;

  22. Algoritma & Pemrograman 1 Mendapatkan Input dari Keyboard: BufferedReader GUIkelas JOptionPane

  23. BufferedReader(import java.io.*) package • Langkah”: • Coding atas +kan import java.io.*; • +kan statement: BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in) ); • Deklarasikan variabel String temporer untuk mendapatkan input, dan gunakan fungsi readLine() untuk mendapatkan input dari keyboard. Anda harus mengetikkannya di dalam blok try-catch: try{ String temp = dataIn.readLine(); } catch( IOException e ){ System.out.println(“Error in getting input”); }

  24. BufferedReader(import java.io.*) package import java.io.*; public class GetInputFromKeyboard { public static void main(String[] args) { BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in) ); String name = ""; System.out.print("Please Enter Your Name:"); try{ name = dataIn.readLine(); }catch( IOException e ){ System.out.println("Error!"); } System.out.println("Hello " + name +"!"); } }

  25. GUIkelas JOptionPanejavax.swing package import javax.swing.*; public class GetInputFromKeyboard1 { public static void main(String[] args) { String name = ""; name=JOptionPane.showInputDialog("Masukkan Nama Anda:"); String msg = "Hello " + name + "!"; JOptionPane.showMessageDialog(null, msg); } }

  26. Tugas 1: 1. Nilai Ambil tiga nilai ujian dari user dan hitung nilai rata-rata dari nilai tersebut. Berikan output rata-rata dari tiga ujian. Berikan juga smiley face pada output jika nilai rata-rata lebih besar atau sama dengan 60, selain itu beri output :-(. 1. Gunakan BufferedReader untuk mendapat input dari user, dan System.out untuk output hasilnya. 2. Gunakan JOptionPane untuk mendapat input dari user dan output hasilnya. 2. Cetak Seratus Kali Buat sebuah program yang mencetak nama Anda selama seratus kali. Buat tiga versi program ini menggunakan while loop, do while dan for-loop.

  27. Tugas 2: 3. Membaca Bilangan Ambil sebuah angka sebagai input dari user, dan outputnya berupa kata yang sesuai dengan angka. Angka yang dimasukkan antara 1-10. Jika user memasukkan nilai yang tidak sesuai berikan output “Invalid number”. 1. Gunakan statement if-else untuk menyelesaikan 2. Gunakan statement switch untuk menyelesaikan 4. Perpangkatan Hitung pangkat sebuah nilai berdasarkan angka dan nilai pangkatnya. Buat tiga versi dari program ini menggunakan while loop, do-while dan for-loop.

More Related