Repetition Control Structure

advertisement
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.
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
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
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
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)
MATERI BELAJAR
STRUKTUR DO WHILE
B. Algoritma Solusi
Fahrenheit_Celcius_Conversion
1
Set temperature_count to zero
2
DOWHILE temperature_count < 15
3
Prompt operator for f_temp
4
Get f_temp
5
Compute c_temp = (f_temp – 32) * 5/ 9
6
Display c_temp
7
Add 1 to temperature_count
8
ENDDO
9
Display “All temperatures processed” to the screen
END
MATERI BELAJAR
STRUKTUR DOWHILE
C. DESK CHECKING
1. Data Input
F_temp
Data 1
Data 2
32
50
2. Hasil yang diharapkan
C_temp
Data 1
Data 2
0
10
MATERI BELAJAR
STRUKTUR DO WHILE
•
Tabel Check
Perintah
No
Temperatu
re_count
1
0
2
DOWHILE
Condition
F_temp
C_temp
True
3,4
32
5
0
6
Display
1
1
2
True
3,4
50
5
10
6
Display
7
2
MATERI BELAJAR
STRUKTUR DOWHILE
•
Penggunaan DOWHILE untuk
perulangan dengan jumlah yang belum
pasti
• 1. Diulang selama nilai tertentu
Co :
Print_examination_scores
1
2
Set total_score to zero
Set total_students to zero
3
Read name, exam_score
4
DOWHILE exam_score not = 999
5
6
Add 1 to total_students
Print name, exam_score
7
Add exam_score to total_score
8
9
Read name, exam_score
ENDDO
9
IF total_students not = zero THEN
•
•
average_score = total_score / total_students
ENDIF
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
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.
MATERI BELAJAR
STRUKTUR DOWHILE
A. DIAGRAM DEFINISI
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
MATERI BELAJAR
STRUKTUR DOWHILE
B. ALGORITMA SOLUSI
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
•
•
•
•
•
•
•
•7
increment total_females_enrolled
ELSE
increment total_males_enrolled
ENDIF
ENDIF
Read Student_records
ENDDO
Print total_females_enrolled
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.
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
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
MATERI BELAJAR
STRUKTUR COUNTED LOOP
•
Contoh :
1
2
3
4
5
6
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
Display “All temperature processed” to the screen
MATERI BELAJAR
Q & A
Download