1 / 82

Struktur dasar & Perintah dasar

Struktur dasar & Perintah dasar. Prima Dewi P, AAP Ratna , Dodi Sudiana. Outline. Introduction Algoritma , pseudocode Basic program structure User friendly program Basic selection structure using IF Repetition structure using while using for and do..while

Download Presentation

Struktur dasar & Perintah dasar

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. Strukturdasar & Perintahdasar Prima Dewi P, AAP Ratna, DodiSudiana

  2. Outline • Introduction • Algoritma, pseudocode • Basic program structure • User friendly program • Basic selection structure using IF • Repetition structure • using while • using for and do..while • Selection structure using switch • Break and continue statement

  3. Introduction

  4. Pendahuluan • Sebelummembuat program: • Harusmemahamimasalah yang dihadapi definisidandeskripsimasalah • Membuatperencanaan yang baik (Algoritma) untukmenyelesaikannya.

  5. Algoritma • Permasalahankomputasi • Dapatdiselesaikandenganmejalankansekumpulankegiatandalamurutantertentu. • DengancaraPengendalian Program

  6. Pseudocode • Pseudocode • Bahasa buatan yang tidak formal dimana dapat membantu untuk mengembangkan algoritma • Dapat menggunakan bahasa sehari-hari • Tidak dapat dijalankan dengan komputer • Membantu ‘membuat’ program sebelum membuat codingnya. • Mudah untuk diterjemahkan ke dalam bentuk bahasa pemrograman

  7. Perintah Dasar • Menyatakanalgoritma • Alurpemecahanmasalah • Mudahdiimplementasidenganbahasapemrograman • Singkat, jelas, terstruktur, fleksibel • Konseppengolahan data berbasisKomputer

  8. PerintahDasardalampseudocode Perintah-perintahdasar yang dapatdigunakanpadapseudocode PerintahLambang penulisantulis/printf/write pembacaanbaca/scanf/read pemuatan (assignment) = pengulangan while…(do)-ewhile pencabanganif…(then)-else-eif

  9. Struktur Dasar • BohmdanJacopini Seluruhprogram dapatditulisdalam 3 strukturpengendali: • Strukturberurut (sequence structure) • Strukturseleksi/percabangan (selection structure): if, if…else, and switch • Strukturpengulangan (repetition structure): while, do…while and for

  10. User friendly program

  11. Program yang akrab (User friendly) • Komunikatif • Mudahdigunakan (memberikanFasilitasKemudahanbagipemakai) • Petunjuksingkatpenggunaan program • Pernyataan error • Cara mengatasikesalahan • Fasilitas Help • Singkat, jelas, terstrukturdanfleksibel

  12. Hal-halpenting yang dapatmembuat program User friendly • Tujuan/Judul program • Petunjukpengoperasian program • Pesankesalahan • KeteranganSiapPanggil • Tataletakperagaan • Hematkegiatan • Fleksibel

  13. Contoh program yang user friendly /*Peragakanpetunjukpengoperasian*/ Write (‘Menghitungrata-rata bilanganpositif \n ======================================= \n - Masukkansembarangbil. Positifatau \n negatifdiakhiridengan <enter> \n - Masukkannoluntukmenandakanbilangan\n terakhir \n ----------------------------------------\n’) /*Inisialisasi*/ n = 0 jumlah = 0

  14. /*Penjumlahanbilanganpositif*/ read (bil) while (bil<> 0) do if (bil > 0) then jumlah = jumlah + bil n = n + 1 eif read (bil) Ewhile /*Penulisanhasilperhitungan*/ If (n <>) 0 then write (n, jumlah, jumlah/n) eif

  15. Basic selection structure

  16. Proses A Proses B Proses C Strukturberurut (sequence structure) • Eksekusi urutan/pernyataan tidak bersyarat

  17. Strukturseleksi/percabangan(selection structure) • Tidaksetiapbaris program akandikerjakan • Hanya yang memenuhisyarat (kondisi) • Syarat/kondisiterdiridari operand-operand yang dihubungkandengan operator relasidan operator logika • Pengalihanpengendalian • Bilaperintah yang dijalankanadalahbukanperintahberikutnyadalamurutanprogram • Hindariperintah ‘goto’  menimbulkanbanyakmasalah

  18. Strukturseleksi/percabangan(selection structure) • Kondisi/syaratberisipernyataanBoolean, yang dapatbernilaibenar (true) atausalah (false) • Menggunakanperintah: • if • If else • switch

  19. Pernyataan Seleksi if • Digunakanuntukmemilihaluralternatif • ContohPseudocode: If nilaimahasiswalebihbesaratausamadengan 60 Print “lulus” • Jikakondisitrue (benar) • Pernyataan Print dijalankandan program akanmeneruskankeperintahberikutnya. • Jikafalse, pernyataan print diabaikandanmenjalankanperintahberikutnya.

  20. Mengubahpseudocode if menjadi program C • Pseudocode: If nilaimahasiswalebihbesaratausamadengan60 Print “lulus” • BahasaC: if( grade >= 60 ) printf( "Passed\n" ); Kondisi yang diseleksi

  21. Flowchart if SimbolBerlian (decision symbol) • Digunakanuntukseleksi • Hasilnya: benaratausalah • Mengujikondisidanmengikutijalur yang tepat. Apakah Nilai >= 60 ? Cetak ‘LULUS’ YA TIDAK

  22. Seleksidenganif…else • if • Hanyamelaksanakanaksibilakondisinyaadalahbenar/true • if…else • Melaksanakansuatuaksiuntukkeduakondisi, baikbenar/true atausalah/false

  23. Mengubahpseudocodeif..else menjadi program C • Pseudocode: If nilaisiswalebihbesaratausamadengan 60Print “Lulus” elsePrint “Gagal” • Code C: if ( nilai >= 60 ) printf( “Lulus\n“ ); else printf( “Gagal\n“ );

  24. Flowchart if..else Apakah Nilai >= 60 ? Cetak ‘Gagal’ TIDAK YA Cetak ‘LULUS’

  25. Pernyataanif…elsebersarang • Pernyataanif…elsedidalampernyataanif…else • Strukturinidigunakanapabilaadabanyakkemungkinankondisi yang diseleksi • Satukondisiterpenuhi, yang lain di-skip

  26. Pernyataanif…elsebersarang • Pseudocode If student’s grade is greater than or equal to 90 Print “A”else If student’s grade is greater than or equal to 80 Print “B” else If student’s grade is greater than or equal to 70 Print “C” else If student’s grade is greater than or equal to 60 Print “D” else Print “F”

  27. Jeniskesalahan • Syntax errors • Caught by compiler • Logic errors: • Have their effect at execution time • Non-fatal: program runs, but has incorrect output • Fatal: program exits prematurely

  28. What we have learned so far.. Selection Structure using if if Only performs an action if the condition is true if…else Specifies an action to be performed both when the condition is true and when it is false Nested if..else Test for multiple cases by placing if…else selection statements inside if…else selection statement Once condition is met, rest of statements skipped

  29. Repetition structure using while

  30. Strukturperulangan(Repetition structure) • Sejumlahaksiharusdilaksanakanberulang kali selamakondisibernilaibenar (true) • Dapatdibuatdengan • while • for • do…while (equal to repeat…until)

  31. Repetition Essentials • Counter-controlled repetition • Definite repetition: jumlahperulangandiketahui • Control variable digunakanuntukmenghitungjumlahperulangan • Sentinel-controlled repetition • Indefinite repetition: jumlahperulangantidakdiketahui • Sentinel value digunakanuntukmenyatakanakhir data

  32. Counter-Controlled Repetition • Counter-controlled repetition requires • The name of a control variable (or loop counter) • The initial value of the control variable • An increment (or decrement) by which the control variable is modified each time through the loop • A condition that tests for the final value of the control variable (i.e., whether looping should continue)

  33. Contoh • Sepuluhorangmahasiswamengikutikuis. Range nilai yang diberikanadalah 0-100. User (dosen) akandimintamemasukkannilai yang didapatolehseluruhmahasiswa. Program akanmenentukan rata-rata nilaikuis yang diperoleh

  34. Fig. 3.5 | Pseudocode algorithm that uses counter-controlled repetition tosolve the class average problem.

  35. Counter-Controlled Repetition using While • Format for while repetition initialization;while( loopContinuationTest ) { statement; increment;} • Example: int counter = 1; // control variable & initialization while ( counter <= 10 ) { // repetition condition printf( "%d\n", counter ); ++counter; // increment }

  36. Outline fig03_06.c (1 of 2 ) Counter to control while loop Initialize counter to 1 while loop iterates as long as counter <= 10 Increment the counter

  37. Outline Calculate the average fig03_06.c (2 of 2 )

  38. Sentinel-controlled repetition • Contoh: User (dosen) akandimintamemasukkannilai yang didapatolehseluruhmahasiswa. Range nilai yang diberikanadalah 0-100. Jumlahmahasiswabelumdiketahuti (tidakterbatas). Program akanmenentukan rata-rata nilaikuis yang diperoleh. • Soalinimiripdengancontohsebelumnya. Bedanyahanyalahperulangantidakdiketahui (indefinite loop). Olehkarenaitudibutuhkan sentinel value

  39. Fig. 3.7 | Pseudocode algorithm that uses sentinel-controlled repetition tosolve the class average problem.

  40. Bagaimanacaramenentukan Sentinel? • Nilai sentinel yang ditentukanharusmemilikitipe yang samadengan input yang dimasukkan, namunberadadiluar range nilai yang benar • Nilai input yang dimasukkanberupaangka integer 0-100. Sehinggadapatdipilihnilai -1 yang beradadiluar range nilaimahasiswa

  41. Outline fig03_08.c (1 of 3 ) float type indicates variable can be a non-integer

  42. Outline while loop repeats until user enters a value of -1 fig03_08.c (2 of 3 ) Ensures the user entered at least one grade Converts total to float type Prints result with 2 digits after decimal point

  43. Outline fig03_08.c (3 of 3 )

  44. More repetition structure

  45. Counter-Controlled Repetition using For • Hanyabisadigunakanuntukjumlahperulangan yang diketahui (definite repetition) • Format: for ( initialization; loopContinuationTest; increment or decrement) statement • Example: for( int counter = 1; counter <= 10; counter++ ) printf( "%d\n", counter ); • Prints the integers from one to ten

  46. Counter-Controlled Repetition using For Fig. 4.3|for statement header components.

  47. Outline fig04_02.c for loop begins by setting counter to 1 and repeats while counter <= 10. Each time the end of the loop is reached, counter is incremented by 1.

  48. Fig. 4.4|Flowcharting a typical for repetition statement.

  49. Outline fig04_05.c Note that number has a different value each time this statement is executed

  50. do…whileRepetition Statement • The do…while repetition statement • Similar to the while structure • Condition for repetition only tested after the body of the loop is performed • All actions are performed at least once • Format: • do { • statement; • } while (condition );

More Related