Struktur IF-THEN dan IF-THEN-ELSE (Kontrol Program)

Struktur IF-THEN dan IF-THEN-ELSE (Kontrol Program) harianjaDi dalam pemrograman kita mengenal istilah struktur penyeleksian Kondisi (Conditional Statemaent) dan struktur perulangan (looping). Kedua struktur ini sangat penting dalam mengontrol suatu program yang kita buat. Tanpa menggunakan kontrol ini program yang kita buat tidak berdaya apa-apa. Penyeleksian kondisi atau pernyataan kondisi (conditional statement) merupakan suatu pernyataan yang menganalisa suatu keadaan dan mengambil keputusan  berdasarkan pada hasil analisa itu. Hasil dari penyeleksian jika bernilai benar maka akan dikerjakan instruksi tertentu. Jika salah maka akan dikerjakan instruksi yang lain.
CONTOH:
Terdapat pernyataan bahwa jika cuaca hujan maka” saya tinggal di rumah”, sedang tidak hujan “saya main bola”.

Bentuk strukturnya:

If (cuaca = “hujan” ) then
            tindakan =”tinggal di rumah”
Else
            tindakan =”main bola”
End if

Struktur IF-THEN
Disebut juga dengan branch sturucture

Merupakan struktur percabangan dimana suatu ekspresi akan dikerjakan jika kondisinya terpenuhi. Tetapi jika kondisi tidak terpenuhi maka ekspresi di dalam struktur if tidak akan dijalankan dan blok if akan dilompati serta program akan melakukan tindakan berikutnya.
BENTUK STRUKTURNYA:
            IF (KONDISI) THEN
                …….EKSPRESI……
            END IF
Kondisi:
Perbandigan antara satu variabel/konstanta dengan variabel/konstanta lain   dengan menggunakan tanda <, >, =, <>, >=, <=.
Ekspresi:
Rangkaian kode program (blok program) yang akan dijalankan jika kondisinya bernilai benar (memenuhi syarat).

Contoh:
Buat suatu program yang akan menyeleksi suatu inputan nilai akhir mahasiswa. Inputan nilai dilakukan lewat inputbox melalui prosedur form_click. Lakukan seleksi pada nilai tersebut yaitu jika nilainya lebih besar atau sama dengan 55  cetak keterangan”lulus”. Tetapi jika tidak  terpenuhi program akan berakhir. Asumsikan inputan nilai antara 0 sampai 100.
Private sub form_click()
      dim nilai_akhir as integer
      nilai_akhir = inputbox(“ketik nilai akhir”)
      if (nilai_akhir) >= 55 then
                  print “nilai_akhir : “ & nilai_akhir
                  print “keterangan : lulus “
      end if
End sub


Struktur IF-THEN-ELSE
Disebut juga dengan selection structure

Merupakan struktur percabangan dimana suatu ekspresi akan dikerjakan jika kondisinya terpenuhi. Jika tidak terpenuhi maka ekspresi yang lain akan dikerjakan.

Struktur if then else sbb:
            if (kondisi) then
                        ….ekspresi 1….
            else
                        ….ekspresi 2…..
            end if

Ekspresi 2: rangkaian kode program(blok program) yang akan dijalankan jika kondisinya bernilai salah (tidak memenuhi syarat)
Contoh:
Buat suatu program yang akan menyeleksi suatu inputan nilai akhir mahasiswa. Inputan nilai dilakukan lewat inputbox melalui prosedur form_click. Lakukan seleksi pada nilai tersebut yaitu jika nilainya lebih besar atau sama dengan 55  cetak keterangan”lulus”. Tetapi jika tidak  terpenuhi cetak keterangan “gagal”. Asumsikan inputan nilai antara 0 sampai 100.
private sub form_click()
       dim nilai_akhir as integer
       nilai_akhir = inputbox(“ketik nilai akhir”)
       if (nilai_akhir) >= 55 then
                   print “nilai_akhir : “ & nilai_akhir
                   print “keterangan : lulus “
       else
                   print “keterangan : gagal “
       end if
end sub
Pada kondisi tertentu di dalam struktur if then else bisa ditempatkan struktur if then atau if then else yang lain. Bentuk semacam ini biasa disebut if tersarang atau nested if.
Bentuk penulisan dari struktur ini adalah sebagai berikut:

If (kondisi 1) then
            if (kondisi 2)  then
                        …ekspresi 1…
            else
                        …ekspresi 2
            end if
Else
            ekspresi 3
End if
Atau
If (kondisi 1) then
            if (kondisi 2)  then
                        …ekspresi 1…
            else
                        …ekspresi 2
            end if
Else
            if (kondisi 3) then
                        …ekspresi 3…
            else
                        …ekspresi 4…
End if
End if

Penulisan di atas dapat juga digabungkan antara else dan if untuk kondisi yang ketiga dan dengan membuang  satu end if, seperti berikut:
If (kondisi 1) then
            if (kondisi 2)  then
                        …ekspresi 1…
            else
                        …ekspresi 2
            end if
Else if (kondisi 3) then
                        …ekspresi 3…
            else
                        …ekspresi 4…
End if

Keterangan:
Ø  Kondisi 1:Berisi perbandingan antara satu variabel/konstanta dg variabel lain dengan menggunakan tanda >,<,<>,=,>=,<=
Ø  Kondisi 2 : akan dikerjakan jika nilai kondisi 1 benar(terpenuhi)
Ø  Kondisi 3: akan dikerjakan jika nilai kondisi 1 adalah salah(tdk terpenuhi)
Ø  Ekspresi 1 : rangkaian kode program(blok program) yang akan
                     dijalankan jika kondisi 1  dan 2 bernilai benar(memenuhi syarat)
Ø  Ekspresi 2 : rangkaian program yang akan dijalankan jika kondisi 1 benar
          dan kondisi 2 bernilai salah
Ø  Ekspresi 3 : rangkaian program yang akan dijalankan jika kondisi 1  salah
          tetapi kondisi 3 bernilai benar
Ø  Ekspresi 4 : rangkaian program yang akan dijalankan jika kondisi 1 salah

          dan kondisi 3 juga salah

Contoh:
Buat sebuah program menyeleksi jenis kelamin dan status perkawinan seseorang dimana keduanya diinputkan lewat inputbox. Jika seseorang jenis kelaminnya “pria”, lakukan seleksi apakah “sudah menikah”. Jika keduanya terpenuhi, tampilkan “ayah” sedangkan jikat tidak terpenuhi tampilkan ”jejaka”. Tetapi jika jenis kelaminya “wanita/bukan pria”, lakukan pengecekan apakah statusnya “sudah menikah”. Jika keduanya terpenuhi, tampilkan “ibu” sedangkan tidak terpenuhi tampilkan “perawan”. Ketik listing programnya pada prosedur form_click seperti berikut:
Dim jenis as string
Dim status as string
Jenis = inputbox (“jenis kelamin”)
Status = inputbox (“sudah menikah”)
Print “jenis kelamin : “ & jenis
Print “sudah menikah : “ & status
Print “keterangan : “;
If jenis  = “pria” then
            if status = “sudah” then
                        print “ayah”
            else
                        print “jejaka”
            end if
Else
            if status =”sudah” then
                        print “ibu”
            else
                        print “perawan”
            end if
End if


Contoh Pemakaian Dalam Program (Program Login)
Private sub command1_click()
If tuser.text <> "" and tpas.text <> "" then
    set rslogin = new adodb.recordset
        rslogin.locktype = adlockoptimistic
        rslogin.cursortype = adopendynamic
        rslogin.open "select * from login where user_name = '" & tuser.text & "' and password = '" & tpas.text & "'", conn, , , adcmdtext
        if not rslogin.eof then
        msgbox ("login anda berhasil")
        form1.show
        login.visible = false
        else
            msgbox "user name dan password salah silahkan ulangi kembali", vbcritical
            exit sub
        end if
End if
End sub

Subscribe to receive free email updates:

27 Responses to "Struktur IF-THEN dan IF-THEN-ELSE (Kontrol Program)"

  1. I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
    tally training in chennai

    hadoop training in chennai

    sap training in chennai

    oracle training in chennai

    angular js training in chennai




    ReplyDelete
  2. You need to participate in a challenge for probably the best webpage on the web. I will enthusiastically suggest this site!
    Paint

    ReplyDelete
  3. In the wake of going over a modest bunch of the blog articles on your site, I really appreciate your procedure of composing a blog. I bookmarked it to my bookmark site page list and will return sooner rather than later. Kindly look at my site also and let me understand your opinion.
    gadget media

    ReplyDelete
  4. Replies

    1. Hello
      Please i just took up macaw breeding as a hobby after my mom passed away because they were her favorite birds. Despite the fact that they are very intelligent, am finding it very difficult getting them to mate.
      For any information CLICK HERE http://supremeparrotfarm.company.com/ we shall get back to you. contact-about-us. THANKS

      Delete
  5. Ucuz, kaliteli ve organik sosyal medya hizmetleri satın almak için Ravje Medyayı tercih edebilir ve sosyal medya hesaplarını hızla büyütebilirsin. Ravje Medya ile sosyal medya hesaplarını organik ve gerçek kişiler ile geliştirebilir, kişisel ya da ticari hesapların için Ravje Medyayı tercih edebilirsin. Ravje Medya internet sitesine giriş yapmak için hemen tıkla: https://www.ravje.com

    İnstagram takipçi satın almak için Ravje Medya hizmetlerini tercih edebilir, güvenilir ve gerçek takipçilere Ravje Medya ile ulaşabilirsin. İnstagram takipçi satın almak artık Ravje Medya ile oldukça güvenilir. Hemen instagram takipçi satın almak için Ravje Medyanın ilgili sayfasını ziyaret et: instagram takipçi satın al

    Tiktok takipçi satın al istiyorsan tercihini Ravje Medya yap! Ravje Medya uzman kadrosu ve profesyonel ekibi ile sizlere Tiktok takipçi satın alma hizmetide sunmaktadır. Tiktok takipçi satın almak için hemen tıkla: tiktok takipçi satın al

    ReplyDelete
  6. Ucuz, kaliteli ve organik sosyal medya hizmetleri satın almak için Ravje Medyayı tercih edebilir ve sosyal medya hesaplarını hızla büyütebilirsin. Ravje Medya ile sosyal medya hesaplarını organik ve gerçek kişiler ile geliştirebilir, kişisel ya da ticari hesapların için Ravje Medyayı tercih edebilirsin. Ravje Medya internet sitesine giriş yapmak için hemen tıkla: www.ravje.com

    İnstagram takipçi satın almak için Ravje Medya hizmetlerini tercih edebilir, güvenilir ve gerçek takipçilere Ravje Medya ile ulaşabilirsin. İnstagram takipçi satın almak artık Ravje Medya ile oldukça güvenilir. Hemen instagram takipçi satın almak için Ravje Medyanın ilgili sayfasını ziyaret et: instagram takipçi satın al

    Tiktok takipçi satın al istiyorsan tercihini Ravje Medya yap! Ravje Medya uzman kadrosu ve profesyonel ekibi ile sizlere Tiktok takipçi satın alma hizmetide sunmaktadır. Tiktok takipçi satın almak için hemen tıkla: tiktok takipçi satın al

    İnstagram beğeni satın almak için Ravje medya instagram beğeni satın al sayfasına giriş yap, hızlı ve kaliteli instagram beğeni satın al: instagram beğeni satın al

    Youtube izlenme satın al sayfası ile hemen youtube izlenme satın al! Ravje medya kalitesi ile hemen youtube izlenme satın almak için tıklayın: youtube izlenme satın al

    Twitter takipçi satın almak istiyorsan Ravje medya twitter takipçi satın al sayfasına tıkla, Ravje medya güvencesi ile organik twitter takipçi satın al: twitter takipçi satın al

    ReplyDelete
  7. Dachshunds are bred and shown in two sizes: Standard and Miniature.https://www.cutespupsforsale.com/ Standard Dachshunds of all varieties (Smooth, Wirehair, and Longhair) usually weigh between 16 and 32 pounds. Miniature Dachshunds of all varieties weigh 11 pounds and under atteacup poodle for sale maturity. Dachshunds that weigh between 11 and 16 pounds are called Tweenies. While this isn't an official classification, Tweenies are not penalized in the show ring. Some people who breed exceptionally small Dachshunds advertise them as Toy Dachshunds, but this is purely a poodles for salemarketing term, not a recognized designation.The Dachshund is described as clever, lively, and courageous to the point of rashness. He's bred for perseverance, which is another way of saying that he can be stubborn. Dachshunds have a reputation for being dachshund puppies saleentertaining and fearless, but what they want most is to cuddle with their people. For many Dachshund people, this characteristic outweighs having to deal with the breed's insistence on having his own way. The Dachshund personality can also vary with coat type. Because the wirehaired Dachshunds have terrier in their background, they can be mischievous troublemakers. Longhairs are calm and quiet, and Smooths havedachshund for sale a personality that lies somewhere in between. Some Mini Dachshunds can be nervous or shy, but this isn't correct for the breed. Avoid puppies that show these characteristics.Like every dog, Dachshunds need early socialization-exposure to many different people, dachshund puppies for sale near mesights, sounds, and experiences-when they're young. Socialization helps ensure that your Dachshund puppy grows up to be a well-rounded dog. Enrolling him in a puppy kindergarten class is a great start. Inviting visitors over regularly, and taking him to busy parks, stores that allow dogs, and on leisurely strolls to meet neighbors will also help him polish his social skills.

    ReplyDelete
  8. Thanks for sharing your thoughts. I truly appreciate your efforts and I will be waiting for your further
    write ups thanks once again
    pomeranian puppies for sales


    This information is invaluable. Where can I find out more?
    pomeranian for sale


    This is the right webpage for anyone who hopes to understand this topic.
    You realize a whole lot its almost hard to argue with you (not that I really would want to…HaHa).
    You definitely put a fresh spin on a topic that’s been discussed for a long
    time. Great stuff, just excellent!
    teacup pomeranian for sale near me


    It’s amazing to pay a quick visit this website and
    reading the views of all colleagues concerning this article,
    while I am also zealous of getting familiarity.
    pomeranians for sale near me


    I take pleasure in, result in I discovered exactly what I used to be taking a look
    for. You’ve ended my 4 day lengthy hunt! God Bless you man. Have a nice
    day. Bye
    pomeranian teacup for sale

    ReplyDelete
  9. At this time I am going away to do my breakfast, afterward having my breakfast coming over again to read further news
    ragdoll cat for sale


    Usually I don’t read post on blogs, but I wish to say that this write-up very compelled me to try and do it!
    Your writing taste has been surprised me. Thank you, very nice article.
    ragdoll cats for sale

    Hi, I do think this is a great blog. I stumbledupon it �� I may come back once again since I saved as a favorite it.
    Money and freedom is the greatest way to change, may you be rich and continue
    to guide others.
    aussiedoodle for sale

    Wonderful article! That is the kind of info that are supposed to be shared around the net.
    Shame on the seek engines for now not positioning this publish upper!
    Come on over and discuss with my website . Thank
    you =)
    bernedoodles for sale

    What’s Going down i’m new to this, I stumbled upon this I have found It positively useful and it has aided me out loads.
    I hope to give a contribution & help different users like its aided me.
    Good job.
    goldendoodle for sale

    ReplyDelete
  10. WhatsApp Plus v19.32 Crack application that is messaging for Android system only. This application works on your phones that are different. Whatsapp Plus Crack

    ReplyDelete
  11. It is a strong and collective item which is established on and utilized by loads of people in the world. that asks for can move all Microsoft items easily utilizing a single tick for essentially life use. https://cyberspc.com/re-loader-activator-crack/

    ReplyDelete
  12. wow, nambah pengetahuan banget seputar dunia programming. Terimakasih!

    ReplyDelete
  13. Christmas Messages for use in Cards and Letters. May abundant peace and vibrant joy be yours this Christmas season; Wishing you a wonderful holiday season Christmas Wishes For Love

    ReplyDelete
  14. Kindly Visit my website for cracked software, Windows & Mac. Download Now Latest Versions.
    Microsoft Office 2019 Crack

    ReplyDelete
  15. The Vivo Y22s is a mid-range smartphone released in December 2020. It has a 6.51-inch IPS LCD display with a resolution of 720 x 1600 pixels. It is powered by a MediaTek Helio P35 chipset with 4 GB of RAM and 128 GB of internal storage.
    vivo y22s price and full specs

    ReplyDelete