Materi 4 SELECTION CONTROL STRUCTURES Disusun Oleh Yulyani




























- Slides: 28
Materi 4 SELECTION CONTROL STRUCTURES Disusun Oleh : Yulyani Arifin, S. Kom, MMSI
Agenda • Struktur kontrol seleksi • Algoritma dengan Seleksi • Struktur Case
Kemampuan Akhir yang Diharapkan • Mahasiswa mampu menerapkan struktur kontrol seleksi baik single maupun multiple dalam algoritma. • Mahasiswa mampu mengembangkan algoritma dengan menggunakan struktur kontrol seleksi yang sesuai.
MATERI BELAJAR STRUKTUR KONTROL SELEKSI • Digunakan pada saat membuat pilihan antara dua atau lebih perintah tergantung pada kondisi true atau false. • Ekspresi yang digunakan untuk perbandingan : < Less Than > Greather Than <= Less Than or Equal To = equal to >= Greather Than or Equal To <> Not Equal To
MATERI BELAJAR STRUKTUR KONTROL SELEKSI • Berbagai variasi Struktur Seleksi : 1. Simple Selection ( Simple IF Statement) - Digunakan hanya untuk dua pilihan saja - Keyword : IF, THEN, ELSE, ENDIF - Contoh : - IF Account_Balance < Rp 10000 THEN Service_charge = 0, 10 ELSE Service_Charge = 0, 15 ENDIF
MATERI BELAJAR STRUKTUR KONTROL SELEKSI 2. Simple Selection with Null Statement - Variasi dari simple selection - Perintah hanya dikerjakan jika kondisi = True. - Contoh : IF student_attendace = True THEN Add 1 to Student_count ENDIF
MATERI BELAJAR STRUKTUR KONTROL SELEKSI 3. Combined Selection - Digunakan jika terdiri dari beberapa kondisi yang digabungkan dengan perintah AND, atau OR - Contoh : IF Student_attendance = True AND Student_gender = Female THEN Add 1 to Student_Count ENDIF
MATERI BELAJAR STRUKTUR KONTROL SELEKSI - The NOT Operator - Operator NOT dapat digunakan untuk negasi dari sebuah kondisi - Contoh : IF NOT ( Student_attendance = False) THEN Add 1 to Student_Count ENDIF
MATERI BELAJAR STRUKTUR KONTROL SELEKSI 4. Nested Selection - Hanya terjadi jika kata IF digunakan lebih dari satu kali - Ada dua jenis : - Linear Nested IF Statement - Non Linear Nester IF Statement
MATERI BELAJAR STRUKTUR KONTROL SELEKSI - Linear Nested IF Statement - Terjadi jika pengecekan hanya pada satu field dengan beragam value Contoh : IF Grade = ‘A’ THEN increment counter_A ELSE IF Grade = ‘B’ THEN increment counter_B ELSE IF Grade = ‘C’ THEN increment counter_C ELSE increment counter_D ENDIF
MATERI BELAJAR STRUKTUR KONTROL SELEKSI - Non-Linear Nested IF Statement • Digunakan jika pengecekan berbagai field dengan beragam kondisi •
MATERI BELAJAR STRUKTUR KONTROL DASAR • Contoh : IF Jurusan = “TI” THEN IF Sesi = “ 01” THEN IF Grade = ‘A’ THEN Add 1 to Ctr_A_TI ELSE Add 1 to other ENDIF ELSE IF Grade = ‘A’ THEN Add 1 to CTR_A_TI_other ENDIF
MATERI BELAJAR ALGORITMA DENGAN SELEKSI • Latihan 1 : Read Three Character Design an algorithm that will prompt a terminal operator for three character, accept those characters as input, sort them into ascending sequence and output them to the screen.
MATERI BELAJAR ALGORITMA DENGAN SELEKSI A. Diagram Definisi Proses Input • char 1 • char 2 • char 3 • Prompt for character • Accept three character • Sort three character • Output three character Output • char 1 • char 2 • char 3
MATERI BELAJAR ALGORITMA DENGAN SELEKSI B. Algoritma Solusi
MATERI BELAJAR ALGORITMA DENGAN SELEKSI C. Desk Checking 1. Data input Data 1 Data 2 Char 1 K Z Char 2 B S Char 3 G A
MATERI BELAJAR ALGORITMA DENGAN SELEKSI C. Desk Checking 2. Hasil yang diharapkan Data 1 Data 2 Char 1 B A Char 2 G S Char 3 K Z
MATERI BELAJAR ALGORITMA DENGAN SELEKSI C. Desk Checking 3. Tabel cek Perintah No Char 1 Char 2 Char 3 1, 2 K B G 3 B K Temp Data pertama 4 5 G 6 Output K G K Output K
MATERI BELAJAR ALGORITMA DENGAN SELEKSI C. Desk Checking 3. Tabel cek Perintah No Char 1 Char 2 Char 3 1, 2 Z S A 3 S Z Temp Data Kedua 4 A 5 A S 6 Output Z Z Z S Output
MATERI BELAJAR STRUKTUR CASE • • Salah satu alternatif untuk menyatakan linear nested IF Statement. Contoh : CASE OF record IF record = ‘A’ THEN add 1 to Ctr_A ELSE IF record = ‘B’ THEN Add 1 to Ctr_B ELSE IF record = ‘C’ THEN Add 1 to Ctr_C ELSE Add 1 to Ctr_D ENDIF ‘A’ : add 1 to Ctr_A ‘B’ : add 1 to Ctr_B ‘C’ : add 1 to Ctr_C Other : add 1 to Ctr _D ENDCASE
MATERI BELAJAR STRUKTUR CASE • Latihan 2 : Process Customer Record A Program is required to read a customer’s name, a purchase amount and a tax code. The tax code has been validated and will be one of the following : 0 : tax exempt (0%) 1 : state sales tax only (3%) 2 : federal and state sales tax (5%) 3 : special sales tax (7%) The program must then compute the sales tax and the total amount due and print the customer’s name, purchase amount, sales tax and total amount due.
MATERI BELAJAR STRUKTUR CASE A. Diagram Definisi Proses • cust_name Input • purch_amt • tax_code • Read customer details • Calculate sales tax • Calculate total amount • Print Customer Details Output • • cust_name purch_amt sales_tax total_amt
MATERI BELAJAR STRUKTUR CASE B. Algoritma Solusi
MATERI BELAJAR STRUKTUR CASE C. Desk Checking 1. Data input Data 1 Data 2 purch_amt 10 20 tax_code 0 2
MATERI BELAJAR STRUKTUR CASE C. Desk Checking 2. Hasil yang diharapkan Data 1 Data 2 sales_tax 0 $1 total_amt $ 10 $ 21
MATERI BELAJAR STRUKTUR CASE C. Desk Checking 3. Tabel cek Perintah No purch_amt tax_code $ 10 0 sales_tax total_amt Data I 1 2 0 3 4 $ 10 Print
MATERI BELAJAR STRUKTUR CASE C. Desk Checking 3. Tabel cek Perintah No purch_amt tax_code $ 20 2 sales_tax total_amt Data II 1 2 1 3 4 $ 21 Print
MATERI BELAJAR Q & A