REPETITION CONTROL STRUCTURES Materi 5 REPETITION CONTROL STRUCTURES Disusun Oleh : Yulyani Arifin,S.Kom, MMSI
Agenda Struktur DO While Struktur Repeat..Until Struktur Counted Loop
Kemampuan Akhir yang Diharapkan Mahasiswa mampu menerapkan struktur kontrol perulangan dalam algoritma. Mahasiswa mampu mengembangkan algoritma dengan menggunakan berbagai variasi struktur kontrol perulangan yang sesuai.
pada awal Loop(leading decision loop) MATERI BELAJAR STRUKTUR DO WHILE Ada tiga cara untuk melakukan perulangan tergantung penempatan kondisi perulangan : pada awal Loop(leading decision loop) pada akhir Loop (trailing decision loop) sesuai jumlah perulangan
Struktur DO WHILE termasuk Leading Decision Loop MATERI BELAJAR STRUKTUR DO WHILE Struktur DO WHILE termasuk Leading Decision Loop Ada dua hal penting perlu di perhatikan dalam mengunakan DO WHILE : Kondisi diuji di awal LOOP perlu ada proses inisial untuk set up kondisi sebelum bisa diuji
MATERI BELAJAR STRUKTUR DO WHILE Cara untuk menghentikan LOOP adalah dengan membuat kondisi menjadi FALSE artinya dalam proses harus ada statement yang menjadikan kondisi menjadi FALSE
STRUKTUR DO WHILE Latihan : Konversi Farenheit – Celcius MATERI BELAJAR STRUKTUR DO WHILE Latihan : Konversi Farenheit – Celcius Every day, a weather station receives 15 temperature expressed in degrees Fahrenheit. A Program is to be written that will accept each Fahrenheit temperature, convert it to Celcius, and display the converted temperatures to the screen. After 15 temperatures have been processed, the words ‘All temperature processed’ are to be displayed on the screen
STRUKTUR DO WHILE A Diagram Definisi F_temp ( 15 temperature) C_temp MATERI BELAJAR STRUKTUR DO WHILE A Diagram Definisi Input F_temp ( 15 temperature) Proses Get Fahrenheit temperatures Convert temperatures Display Celsius temperatures Display screen message Output C_temp (15 temperatures)
STRUKTUR DO WHILE B. Algoritma Solusi MATERI BELAJAR Fahrenheit_Celcius_Conversion 1 Set temperature_count to zero 6 Display c_temp 7 Add 1 to temperature_count 5 Compute c_temp = (f_temp – 32) * 5/ 9 4 Get f_temp 2 DOWHILE temperature_count < 15 3 Prompt operator for f_temp END 8 ENDDO 9 Display “All temperatures processed” to the screen
STRUKTUR DOWHILE C. DESK CHECKING Data Input Hasil yang diharapkan MATERI BELAJAR STRUKTUR DOWHILE C. DESK CHECKING Data Input Hasil yang diharapkan Data 1 Data 2 F_temp 32 50 Data 1 Data 2 C_temp 10
STRUKTUR DO WHILE Tabel Check MATERI BELAJAR Perintah No Temperature_count DOWHILE Condition F_temp C_temp 1 2 True 3,4 32 5 6 Display 50 10 7
Penggunaan DOWHILE untuk perulangan dengan jumlah yang belum pasti MATERI BELAJAR STRUKTUR DOWHILE Penggunaan DOWHILE untuk perulangan dengan jumlah yang belum pasti 1. Diulang selama nilai tertentu Co : Print_examination_scores 1 Set total_score to zero 2 Set total_students to zero 3 Read name, exam_score 4 DOWHILE exam_score not = 999 5 Add 1 to total_students 6 Print name, exam_score 7 Add exam_score to total_score 8 Read name, exam_score 9 ENDDO 9 IF total_students not = zero THEN average_score = total_score / total_students ENDIF END
STRUKTUR DOWHILE Diulang sampai EOF MATERI BELAJAR STRUKTUR DOWHILE Diulang sampai EOF ^ Jika tidak ada nilai tertentu,perulangan juga bisa diulang sampai tanda EOF. Biasanya tanda EOF muncul saat file baru dibuat atau charater terakhir dalam sebuah FILE. ^ Syntax yang biasa digunakan : - DOWHILE more data - DOWHILE more records - DOWHILE records exists - DOWHILE NOT EOF
STRUKTUR DOWHILE Contoh : MATERI BELAJAR STRUKTUR DOWHILE Contoh : A program is required that will read a file of student records and select and print only the records of those students enrolled in a course unit named Programming I. Each student record contains student number, name, address, postcode, gender and course unit number. The course unit number for Programing I is 18500. Three totals are to be printed at the end of the report: total females enrolled in the course, total males enrolled in the course and total students enrolled in the course.
A. DIAGRAM DEFINISI STRUKTUR DOWHILE MATERI BELAJAR Input Student_record Student_no Name Address Postcode Gender Course_unit Proses Read student records Select student records Print Selected Records Computer total females enrolled Compute total males enrolled Compute total students enrolled Print Totals Output Selected student_records Total_females_enrolled Total_males_enrolled Total_students_enrolled
B. ALGORITMA SOLUSI STRUKTUR DOWHILE MATERI BELAJAR Process_student_enrollements 1 Set total_females_enrolled to zero 2 Set total_males_enrolled to zero 3 Set total_students_enrolled to zero 4 Read student_records 5 DOWHILE record_exist 6 IF course_unit = 18500 THEN Print student details Increment total_students_enrolled IF student_gender = Female THEN increment total_females_enrolled ELSE increment total_males_enrolled ENDIF Read Student_records ENDDO 7 Print total_females_enrolled 8 Print total_males_enrolled END
Struktur Repeat Until menguji kondisi pada akhir LOOP. MATERI BELAJAR STRUKTUR REPEAT UNTIL Struktur Repeat Until menguji kondisi pada akhir LOOP. Perintah di dalam LOOP minimal akan dikerjakan 1x sebelum kondisi diuji. Jika kondisi = FALSE maka perulangan akan dilakukan sampai kondisi = TRUE.
Process_student_records MATERI BELAJAR STRUKTUR REPEAT UNTIL Contoh : Process_student_records Set student_score to zero REPEAT Read student record IF student_number NOT = 999 THEN Write Student_record Increment student_count ENDIF Until student_number = 999 Print student_count END
Proses LOOP dikontrol oleh Index Loop Syntax : MATERI BELAJAR STRUKTUR COUNTED LOOP Struktur Counted Loop digunakan jika jumlah perulangan sudah diketahui. Proses LOOP dikontrol oleh Index Loop Syntax : DO Loop_Index = initial value to final_value Statement Block ENDDO
Contoh : STRUKTUR COUNTED LOOP Fahrenheit_Celcius_conversion MATERI BELAJAR STRUKTUR COUNTED LOOP Contoh : Fahrenheit_Celcius_conversion DO temperature_count = 1 to 15 Prompt operator for f_temp Get f_temp Compute c_temp = (f_temp – 32)* 5/9 Display c_temp ENDDO 6 Display “All temperature processed” to the screen
MATERI BELAJAR Q & A