Senin, 07 Maret 2011

Selection

Selection
Struktur Kontrol Selection / Pemilihan
     Ada beberapa variasi dari struktur kontrol selection yaitu:

  • Simple Selection (simple IF Statement)
  • Simple Selection tanpa cabang
  • Combined Selection
  • Nested Selection
  • Linear Nested IF Statement
  • Non-Linear IF Statement

1. Simple Selection
Simple selection terjadi jika harus memilih diantara dua alternatif yang ada, tergantung dari hasil kondisi apakah True atau false.
Keyword yang digunakan adalah : IF, THEN, ELSE, dan END IF

Contoh
IF saldo < $300 THEN
    bunga = 0.05
ELSE
    bunga = 0.1
ENDIF

2. Simple Selection Tanpa Cabang
Simple selection ini terjadi jika sebuah statement hanya bisa dikerjakan bila kondisinya adalah TRUE
Contoh:
IF Saldo > $300 THEN
    bunga = saldo * 0.1
ENDIF
           
3. Combined Selection
        Combined Selection terjadi jika kondisi yang harus diperiksa lebih dari satu. Kondisi tersebut dapat dihubungkan dengan menggunakan AND atau OR.

Contoh:
IF saldo > 300 AND kode = 1 THEN
    bunga = saldo * 0.1
ELSE
    bunga = saldo * 0.05
ENDIF
Statement bunga = saldo *0.1, akan bisa dikerjakan jika kedua kondisi yaitu saldo > 300 dan Kode = 1 bernilai TRUE. Hal ini dikarenakan penghubung yang digunakan adalah AND 

3. Nested Selection 
Tabel kebenaran
Nested selection terjadi, jika di dalam IF terdapat statement IF yang lain.
Ada dua jenis nested selection
Linear Nested IF statement
Non-Linear Nested IF statement
Linear Nested IF Statement
Linear Nested terjadi jika satu kondisi di cek untuk beberapa nilai.
Contoh:
        IF record_code=‘A’ THEN
             increment counter_A
        ELSE
             IF record_code=‘B’ THEN
                     increment counter_B
            ELSE
                     IF record_code=‘C’ THEN
                             increment counter _C
                     ELSE
                             increment error_counter
                    ENDIF
                ENDIF
       ENDIF

Non-Linear Nested IF
Non-Linear Nested IF Statement terjadi jika beberapa kondisi harus diperiksa sebelum suatu statement dikerjakan.
Contoh
IF student_attendance=part_time THEN
        IF student_gender=female THEN
            IF student_age >21 THEN
            add 1 to mature_fem_pt_students
            ELSE
            add 1 to young_fem_pt_students
            ENDIF
         ELSE
            add 1 to male_pt_students
         ENDIF
ELSE
       add 1 to full_time_students
ENDIF




Tugas : buat contoh lain selain contoh di atas untuk :
  • Simple Selection (simple IF Statement)
  • Simple Selection tanpa cabang
  • Combined Selection
  • Nested Selection
  • Linear Nested IF Statement
  • Non-Linear IF Statement

17 komentar:

  1. Sanusi/906/091131011

    +Simple Selection
    IF saldo < $600 THEN
    bunga = 0.6
    ELSE
    bunga = 0.2
    END IF

    +Simple Selection Tanpa Cabang
    IF Saldo > $600 THEN
    bunga = saldo 0.2
    ENDIF

    +Combined Selection
    IF saldo > 600 AND kode = 2 THEN
    bunga = saldo * 0.2

    ELSE
    bunga = saldo * 0.06
    ENDIF

    +nested Selection
    IF record_code=‘D’ THEN
    increment counter_D
    ELSE
    IF record_code=‘E’ THEN
    increment counter_E
    ELSE
    IF record_code=‘F’ THEN
    increment counter _E
    ELSE
    increment error_counter
    ENDIF
    ENDIF
    ENDIF

    + Non Linear IF statement
    if Profesor_attendence=part_time then
    if Profesor_gender=female then
    if Profesor_age>45 then
    add 1 to math_female_pt_Profesor
    else
    add 1 to old_female_pt_Profesor
    end if
    else
    add 1 to male_pt_Profesor
    add 1 to full_time_Profesor
    end if

    BalasHapus
  2. * Simple Selection (simple IF Statement)
    if
    ( boolean_expression )
    {statement1; statement2;...}
    else
    {statement1; statement2; ... )

    * Simple Selection tanpa cabang

    if ( grade >= 60 )
    printf( "Passed\n" );

    * nested Selection

    1. declare variable
    2. get the age
    3. if the age is less than 18
    display "you are too yaoung to vote."
    else
    get the registration status
    if the registration status is Y
    display "you can vote."
    else
    display"you need to register before you can vote."
    endif
    endif

    * Non-Linear Nested IF

    IF Employee-Code = “H”
    THEN
    IF Hours-Worked <= 40
    THEN
    Calculate pay at hourly rate
    ELSE
    Calculate pay with overtime
    ENDIF
    ELSE
    Pay base salary
    ENDIF

    BalasHapus
  3. Hadi Eko Praytino / 091131093
    1. Simple Selection
    IF umur > 56 THEN
    Status = Pensiun
    ELSE
    Status = Active
    END IF
    2. Simple Selection Tanpa Cabang
    IF umur > 56 THEN
    Status = Pensiun
    3. Combined Selection
    IF umur > 56 AND kode = 1 THEN
    Status = Pensiun
    ELSE
    Status = Active
    ENDIF
    4. Linear Nested IF Statement
    IF Tunjangan = Jakarta Then
    Tunjangan = 1.000.000
    ELSE
    IF Tunjangan = Surabaya Then
    Tunjangan = 500.000
    ELSE
    IF Tunjangan = Bandung Then
    Tunjangan = 750.000
    ELSE
    Tunjangan = 0
    ENDIF
    ENDIF
    ENDIF
    5. Non Linear Nested IF Statement
    IF Pekerja_Umur > 56 THEN
    IF Pekeja_Kerja > 1 THEN
    Pay_Bonus_100 %
    ELSE
    Pay_Bonus_50 %
    END IF
    Pay_Bonus_No
    ENDIF

    BalasHapus
  4. Ahmad Syarif / 906 / 091131074

    1.simple selection
    IF nilai UAS > 100 THEN
    mata kuliah = bahasa indonesia
    ELSE
    nilai UAS = active
    END IF
    2.Simple selection Tanpa Cabang
    IF nilai UAS = 100 THEN
    mata kuliah = bahasa indonesia
    3.Combined Selection
    IF nilai UAS = 100 END kode = 1THEN
    mata kuliah = bahasa indonesia
    ELSE
    nilai UAS = aktif
    END IF
    4.Linear Nested IF Statement
    IF perguruan tinggi = Univ.Kristen Indo. THEN
    perguruan tinggi = 100
    ELSE
    IF perguruan tinggi = Univ.Negeri Jakarta THEN
    perguruan tinggi = 65
    ELSE
    IF perguruan tinggi = Univ Muhammadiyah Then
    perguruan tinggi = 85
    ELSE
    perguruan tinggi = 0
    END IF
    END IF
    END IF
    5.Non Linear Nested IF Statement
    IF Mahasiswa_Nilai UAS>100 THEN
    IF Mahasiswa_Mahasiswa>1THEN
    nilai_rata-rata_ 10%
    ELSE
    nilai_rata-rata_ 5%
    ELSE
    END IF
    Nilai_rata-rata_ 0%
    END IF

    BalasHapus
  5. Sugiyarto /906/091131018

    1.Simple Selection
    if overtime (JAM LEMBUR) > 50 jam THEN
    fee non gapok dll = 1jt
    ELSE
    fee non gapok dll = 1,2jt
    ENDIF

    2.Simple Selection Tanpa Cabang
    if overtime > 50 jam THEN
    fee non gapok dll = *1,2jt

    3.Combined Selection
    if overtime > 50jam ANDkode=1THEN
    fee non gapok dll = 1jt
    ELSE
    fee non gapok dll = 1,2jt
    ENDIF

    4.Linier Nested If Statement
    if UMR = DKI Jakarta THEN
    UMR = Rp.1.290.000,-
    ELSE
    if UMR = Bali THEN
    UMR = Rp.1.221.000,-
    ELSE
    if UMR = Gorontalo THEN
    UMR = Rp.762.500,-
    ELSE
    UMR = 0 (error)
    ENDIF
    ENDIF
    ENDIF

    5.Non Linier Nested If Statement
    if narapidana hukuman > 5 thn THEN
    if narapidana berkelakuan baik > 3 thn THEN
    remisi_tahanan_1thn
    ELSE
    remisi_tahanan_2thn
    ELSE
    remisi_tahanan_No
    ENDIF

    BalasHapus
  6. Indriyanto Utama / 091131095 / 906

    1.Simple Selection (simple IF Statement) :
    If Label4.Visible = False Then
    Label4.Visible = True
    Else
    Label4.Visible = False
    End If

    2.Simple Selection tanpa cabang :
    If (Label5.Left + Label5.Width) <= 0 Then
    Label5.Left = Me.Width
    End If

    3.Combined Selection :
    If Label_Angka_Ke_Satu.Caption = 7 Or Label_Angka_Ke_Dua.Caption = 7 Or Label_Angka_Ke_Tiga.Caption = 7 Then
    Timer2.Enabled = True
    Else
    Timer2.Enabled = False
    End If

    4.Linear Nested IF Statement :
    If Text1.Text = "" Then
    MsgBox "Silakan masukkan nilai angka uang di kotak Masukkkan Uang Yang Dibayar ", vbOKOnly, "Peringatan"
    Text1.Text = ""
    ElseIf Val(Text1.Text) = totalharga Then
    Label5.Caption = "Tidak kembali, uang pas"
    kembalian = 0
    ElseIf Val(Text1.Text) < totalharga Then
    kembalian = totalharga - Val(Text1.Text)
    Label5.Caption = "Uang kurang : Rp." & kembalian & ",-"
    ElseIf Val(Text1.Text) > totalharga Then
    kembalian = Val(Text1.Text) - totalharga
    Label5.Caption = "Uang Kembali : Rp." & kembalian & ",-"
    End If

    5.Non-Linear Nested IF Statement :
    If m > 7 Then
    If m Mod 2 = 0 Then
    If d > 31 Or d < 1 Then
    lblHasil.Text() = "tanggal yang anda masukkan tidak valid"
    Else
    lblHasil.Text() = "tanggal yang anda masukkan valid"
    End If
    Else
    If d > 30 Or d < 1 Then
    lblHasil.Text() = "tanggal yang anda masukkan tidak valid"
    Else
    lblHasil.Text() = "tanggal yang anda masukkan valid"
    End If
    End If
    End If

    BalasHapus
  7. amin mauzun 906mi
    assalamualaikum wr.wb.
    1. simple selection
    if umur>17 then
    result=adult
    else
    result=children
    end if
    2.simple selection tanpa cabang
    if umur>17 then
    result=adult
    end if
    3. combined selection
    if umur>17 and kode=1 then
    result=adult
    else
    result=children
    end if
    4.psk jujur aja lebih jauh lagi ane gk tahu,,, sampai sini aja.
    trimakasih
    ,, wassalam.

    BalasHapus
  8. Nama : Euis sutarsih
    Nim : 091130964
    kelas : 906

    1. Simple selection
    IF Jam kerja < 40 THEN
    Gaji = 25.000
    ELSE
    Gaji = 27.000
    ENDIF

    2. Simple selection tanpa cabang
    IF Jam kerja > 40 THEN
    Gaji = Jam kerja * 27.000
    ENDIF

    3. Combined selection
    IF Jam kerja > 40 AND Kode = 01 THEN
    Gaji = Jam kerja * 27.000
    ELSE
    Gaji = Jam kerja * 25.000
    ENDIF
    4. Linear Nested
    IF Nilai = 9,0 - 10 THEN
    Huruf_A
    ELSE
    IF Nilai = 7,6 - 8,9 THEN
    Huruf_B
    ELSE
    IF Nilai = 6,0 - 7,5 THEN
    Huruf_C
    ELSE
    Huruf error
    ENDIF
    ENDIF
    ENDIF

    5. Non_Linear IF Statement
    IF Dosen_attendence = part_time THEN
    IF Dosen_gender = male THEN
    IF Dosen_age > 70 THEN
    Add 1 to mart_male_pt_Dosen
    ELSE
    Add 1 to old_male_pt_Dosen
    ENDIF
    ELSE
    Add 1 to male_pt_Dosen
    Add 1 to Full_time_Dosen
    ENDIF

    BalasHapus
  9. Nama : Rudy yanto bachtiar
    Nim : 091131052
    kelas : 906

    1. Simple selection
    IF totalbayar > 500.000 THEN
    Bunga = 0.05 %
    ELSE
    diskon = 0
    ENDIF

    2. Simple selection tanpa cabang
    IF Totalbayar > 500.000 THEN
    diskon = * 0.5 %
    ENDIF

    3. Combined selection
    IF Totalbayar < 500.000 AND Kode = 01 THEN
    Diskon = * 0.5 %
    ELSE
    Bunga = * 0
    ENDIF

    4.Nested selection
    IF Perumahan = Blok k THEN
    Harga = 500.000.000
    ELSE
    IF Perumahan = Blok B THEN
    Harga = 200.000.000
    ELSE
    IF Perumahan = Blok A THEN
    Harga = 900.000.000
    ELSE
    Harga = 0
    ENDIF
    ENDIF
    ENDIF

    5. maaf pak yang ini saya bingung Nih Piisss...

    BalasHapus
  10. nama:sumiyati
    nim:091131020

    1.simple selection
    IF harga handphone>rp.6000.000 THEN
    merk handphone= blackberry
    ELSE
    IF harga handphone>3000.000 THEN
    merk handphone=SAMSUNG
    END IF
    2.simple selection tanpa cabang
    IF harga mobil>rp.400.000.000 THEN
    merk mobil= AUDI A4
    END IF
    3.combined selection
    IF harga mobil>rp.200.000.000 AND kode = 1 THEN
    merk mobil=toyota alpard
    ELSE
    merk mobil= suzuki swift
    END IF
    4.nested selection
    IF record_code= 'coca cola' THEN
    increment counter coca cola
    ELSE
    IF record_code='fanta' THEN
    increment counter fanta
    ELSE
    IF record_code=sprit THEN
    increment counter sprit
    ELSE
    increment error_counter
    END IF
    END IF
    END IF
    5.non linear nested
    IF waiter part time THEN
    IF waiter_gender female THEN
    IF waiter_age>20 THEN
    add to young_fem_waiter
    END IF
    ELSE
    add to male_waiter
    END IF
    ELSE
    add to full time_waiter
    END IF

    BalasHapus
  11. IMAN SALEH.T 906 NIM 091131040

    1.SIMPLE SELECTION
    IF PRICE <1.000.000 THEN
    DISCOUNT=0,5%
    ELSE DISCOUNT=1%
    ENDIF
    2.SIMPLE SELECTION TAMPA CABANG
    IF PRICE > RP1.000.000 THEN
    DISCOUNT=PRICE*1%
    ENDIF
    3.COMBINED SELECTION
    IF PRICE > 1.000.000 AND KODE= 1 THEN
    DISCOUNT=PRICE* 1%
    ELSE DISCOUNT= PRICE*0.5%
    ENDIF
    4.LINER NESTED IF STATEMENT
    IF PRICE='MANGGA'THEN
    PRICE=MANGGA
    ELSE
    IF PRICE='JERUK' THEN
    PRICE=JERUK
    ELSE
    IF PRICE='RAMBUTAN'THEN
    PRICE=RAMBUTAN
    ELSE
    PRICE=BUAH
    ENDIF
    ENDIF
    ENDIF
    5.NON LINER NESTED IF
    IF WORKER_JOB=FULL TIME THEN
    IF WORKER_JOB=FEMALE THEN
    IF WORKER-AGE>10 THEN
    PAY SALARY-35% WORKER
    ELSE
    PAY SALARY-15% WORKER
    ELSE
    PAY SALARY PART TIME-5% WORKER
    ENDIF

    BalasHapus
  12. Arif Prasetyo_906
    091130953
    (^_^)

    Jawab :

    • Simple Selection (simple IF Statement)
    IF Harga > Rp. 100.000,- THEN
    Discount = 10 %
    Else
    Discount = 15 %
    End If

    • Simple Selection tanpa cabang
    IF Harga > Rp. 100.000,- THEN
    Discount = Harga * 15 %
    End If

    • Combined Selection
    IF Harga > Rp. 100.000,- AND Kode = A THEN
    Discount = Harga * 15 %
    Else
    Discount = Harga * 10 %
    End If

    • Linear Nested IF Statement
    IF Kode_Barang = ‘A’ Then
    Barang_Televisi
    Else
    IF Kode Barang = ‘B’ Then
    Barang_Kulkas
    Else
    IF Kode_Barang = ‘C’ Then
    Barang_Mesin Cuci
    Else
    Barang_Error
    End If
    End If
    End If

    • Non-Linear IF Statement
    IF Pegawai_Bagian = Logistik THEN
    IF Pegawai_ gender= male THEN
    IF Pegawai_Age>25 THEN
    Add 1 to Position_few_Logistik_Pegawai
    Else
    Add 1 to young_few_Logistik_Pegawai
    End If
    Else
    Add 1 to male_Logistik_Pegawai
    End If
    Else
    Add 1 to Full_time_Pegawai
    End If

    BalasHapus
  13. happy anandari_906
    091131037

    1.Simple Selection (simple IF Statement)
    double x; // the input number
    cout << "Enter the number: ";
    cin >> x;

    if (x == 0)
    cout << "is zero";
    else
    if (x > 0)
    cout << "is positive";
    else
    cout << "is negative";
    return 0;
    ENDIF

    2.Simple Selection tanpa cabang
    if (x == 0)
    cout << "is zero";
    else
    {
    if (x > 0)
    cout << "is positive";
    else
    cout << "is negative";
    ENDIF

    3.Combined Selection
    IF saldo >300 AND kode = 1THEN
    bunga = saldo *0.1
    ELSE
    bunga = saldo*0.05
    ENDIF

    4.Linear Nested IF Statement
    IF Kode_Barang = ‘A’ Then
    Barang_buku tulis
    Else
    IF Kode Barang = ‘B’ Then
    Barang_penghapusan
    Else
    IF Kode_Barang = ‘C’ Then
    Barang_penggarisan
    Else
    Barang_Error
    End If
    End If
    End If

    5.non linear nested
    IF SPG part time THEN
    IF SPG_gender female THEN
    IF SPG_age>19 THEN
    add to young_fem_SPG
    END IF
    ELSE
    add to male_SPG
    END IF
    ELSE
    add to full time_SPG
    END IF

    BalasHapus
  14. ANDRI_906
    091131076

    1.simple selection
    IF computer>Rp.2.000.000 THEN
    merk computer= accer
    ELSE
    IF computer>Rp.1.500.000 THEN
    merk computer= HP
    END IF

    2.2.simple selection tanpa cabang
    IF Leptop>Rp.6.500.000 THEN
    merk Leptop= HP
    END IF

    3.combined selection
    IF Leptop>Rp.6.000.000 AND kode = 1 THEN
    merk mobil= DELL
    ELSE
    END IF

    4.Linear Nested IF Statement
    IF code_1= 'perdana' THEN
    increment counter Rapi cell
    ELSE
    IF code_2=Mentari THEN
    ELSE
    IF code_3=IM3 THEN
    ELSE
    IF code_4=ESIA THEN
    ELSE
    IF code_5=FLEXI THEN
    ELSE
    increment error_counter
    END IF
    END IF
    END IF

    5.non linear nested
    IF Karyawan_Bagian = assembling THEN
    IF Karyawan_ gender= male THEN
    IF Karyawan_Age>22 THEN
    Add 1 to_few_assembling_karyawan
    Else
    Add 1 to young_few_assembling_karyawan
    End If
    Else
    Add 1 to male_assembling_karyawan
    End If
    Else
    Add 1 to Full_time_karyawan
    End If

    BalasHapus
  15. -contoh Simple Selection :
    IF sakit kepala then
    obat = bodrex
    ELSE
    If sakit flu then
    obat = sana flu
    ENDIF
    -contoh Simple Selection tanpa cabang :
    IF gaji > 2500000 THEN
    bonus = gaji + lembur
    ENDIF
    -contoh Combined Selection :
    IF gaji > 500000 AND kode = 1 THEN
    bonus = gaji + lembur
    ELSE
    bonus = gaji + 1 jam/20000
    ENDIF
    -contoh Nested Selection :
    IF ukuran=‘m’ THEN
    ukuran= 'medium'
    ELSE
    IF ukuran=‘H’ THEN
    ukuran = 'hard'
    ELSE
    IF ukuran=‘E’ THEN
    ukuran ='easy'
    else
    ENDIF
    ENDIF
    ENDIF
    -contoh Linear Nested IF Statement :
    IF tujuan = Jakarta Then
    tarif = 1000000
    ELSE
    IF Tujuan = Surabaya Then
    tarif = 500000
    ELSE
    IF Tujuan = Bandung Then
    tarif = 750000
    ELSE
    tarif = 500000
    ENDIF
    ENDIF
    ENDIF
    -contoh Non-Linear IF Statement :
    5. Non Linear Nested IF Statement
    IF masa_kerja > 3 THEN
    IF gaji_Kerja > 1 THEN
    gapok/700000+transfort/1500000+makan/200000
    ELSE
    lembur/jam *20000
    END IF
    tunjangan jabatan/300000
    ENDIF
    end if
    end if

    BalasHapus
  16. 1.simple selection
    IF harga handphone>rp.10.000 THEN
    merk handphone= Nokia
    ELSE
    IF harga handphone>30.000 THEN
    merk handphone=nexian
    END IF
    2.simple selection tanpa cabang
    IF harga mobil>rp.400.000.000 THEN
    merk mobil= AUDI A4
    END IF
    3.combined selection
    IF harga mobil>rp.200.000.000 AND kode = 1 THEN
    merk mobil=toyota alpard
    ELSE
    merk mobil= suzuki swift
    END IF
    4.nested selection
    IF record_code= 'coca cola' THEN
    increment counter coca cola
    ELSE
    IF record_code='fanta' THEN
    increment counter fanta
    ELSE
    IF record_code=sprit THEN
    increment counter sprit
    ELSE
    increment error_counter
    END IF
    END IF
    END IF
    5.non linear nested
    IF waiter part time THEN
    IF waiter_gender female THEN
    IF waiter_age>20 THEN
    add to young_fem_waiter
    END IF
    ELSE
    add to male_waiter
    END IF
    ELSE
    add to full time_waiter
    END IF

    BalasHapus
  17. 1. Simple Selection
    IF buy buavita < 3 THEN
    free = 2 ticket dufan
    ELSE
    free = 1 ticket dufan
    ENDIF

    2. Simple Selection Tanpa Cabang
    IF buy buavita > 3 THEN
    free = 1 ticket dufan
    ENDIF

    3. Combined Selection
    IF buy buavita > 3 AND kode = 1 THEN
    free = 1 ticket dufan
    ELSE
    free = 2 ticket dufan
    ENDIF

    4. Nested Selection
    IF record_code=‘C’ THEN
    increment supermart_Carefour
    ELSE
    IF record_code=‘G’ THEN
    increment supermart_Giant
    ELSE
    IF record_code=‘H’ THEN
    increment supermart_Hyper
    ELSE
    increment error_counter
    ENDIF
    ENDIF
    ENDIF

    5.Non-Linear Nested IF
    IF SPG_attendance=part_time THEN
    IF SPG_gender=female THEN
    IF SPG_age >20 THEN
    add 1 to mature_fem_pt_SPG
    ELSE
    add 1 to young_fem_pt_SPG
    ENDIF
    ELSE
    add 1 to male_pt_SPB
    ENDIF
    ELSE
    add 1 to full_time_SPG / SPB
    ENDIF



    Maya Sri Eka
    Nim : 091131043

    BalasHapus