Döngüler

If ..... Then ..... Else ...... End if

 

İf deyimi belli bir ifadenin değerine göre bir deyimin işletilmesini sağlar. İf deyiminin basitten karmaşığa doğru değişik kalıpları vardır.

 

1.

            if (koşul) Then (işlem)

2.

            if (koşul) Then

                        (işlem1)

            Else

                        (işlem2)

            End if

3.

            if (koşul1) Then

                        (işlem1)

            ElseIf (koşul2) Then

                        (işlem2)

            Else

                        (işlem-n)

            End if

 

ÖRNEK 1

 

İki sayı girin ve Ok basınca iki sayının çarpımını Cancel ‘e basınca toplamını alsın ve sonucu MsgBox ile ekranda göstersin.

 

Private Sub Command1_Click()

Dim a As Integer, b As Integer

Dim c As Integer

Dim d As Integer

a = InputBox("1.sayıyı giriniz")

b = InputBox("2.sayıyı giriniz")

c = MsgBox("Sayılar toplansın mı? yoksa çarpılsın mı?", vbOKCancel)

    If c = 1 Then

    d = a * b

    MsgBox ("İki sayının çarpımı =" & d)

    End If

    If c = 2 Then

    d = a + b

    MsgBox ("İki sayının toplamı  =" & d)

    End If

End Sub

 

ÖRNEK 2

 

Bir öğrencinin aldığı notu girelim ve aldığı not < 50 ise ekrana zayıf not aldı , >=50 ise ekrana geçer not aldı yazsın.

 

Private Sub Command1_Click()

Dim a As Integer

a = InputBox("Öğrencinin notunu giriniz")

    If a < 50 Then

    MsgBox ("Bu öğrenci zayıf not aldı")

    End If

    If a >= 50 Then

    MsgBox ("Bu öğrenci geçer not aldı")

    End If

End Sub

 

ÖRNEK 2.1

 

Private Sub Command1_Click()

Dim a As Integer

 

a = InputBox("Öğrencinin notunu giriniz")

 

    If a < 50 Then

            MsgBox ("Bu öğrenci zayıf not aldı")

    Else  

MsgBox ("Bu öğrenci geçer not aldı")

    End If

 

End Sub

 

ÖRNEK 3

 

Klavyeden girilen 3 sayının en büyük olanını bulmak.

 

Private Sub Command1_Click()

Dim a As Double

Dim b As Double

Dim c As Double

 

a = InputBox("birinci sayıyı giriniz")

b = InputBox("ikinci sayıyı giriniz")

c = InputBox("üçüncü sayıyı giriniz")

 

    If a > b Then

        If a > c Then MsgBox "En Büyük sayı birinci sayıdır."

        ElseIf c > b Then MsgBox "En Büyük sayı üçüncü sayıdır"

    Else

        MsgBox "En büyük sayı ikinci sayıdır."

    End If

End Sub

 

ÖRNEK 4

 

Elektrik faturası hesaplayan bir program 150 kw kadar 44.000 TL. 150 – 300 kw arası için 66.000 TL.  300 den sonrası için 75.000 TL. olsun.

 

Private Sub Command1_Click()

Dim tüketim As Double

Dim para As Double

 

tüketim = InputBox("Tüketim Değerini Girin")

    If tüketim > 0 And tüketim < 151 Then

        para = tüketim * 44000

    ElseIf tüketim > 150 And tüketim < 301 Then

        para = (150 * 44000) + ((tüketim - 30) * 66000)

    ElseIf tüketim > 301 Then

        para = (150 * 44000) + (150 * 66000) + ((tüketim - 300) * 75000)

    End If

    MsgBox ("ödeyeceğiniz para =" & para)

End Sub

 

 

 

 

 

 

ÖRNEK 5

 

3 bu örnek de 1.vize ,2. vize ve final notları girilecek ve puanı 50’den büyükse msgbox la geçtiniz değilse bütünleme notunu istet ve sonuc yine 50 den küçükse kalsın değilse geçsin. Vizenin %40 Finalin % 60

 

Dim a As Integer ,b As Integer ,c As Integer, d As Double, e As Double, f As Double, g As Integer, but As Double

a:

a = InputBox("Birinci vize notu giriniz")

    If a < 0 Or a > 100 Then

    MsgBox ("Lütfen 0 - 100 arasında bir sayı giriniz")

    GoTo a

Else

b:

b = InputBox("ikinci vize notu giriniz")

    If b < 0 Or b > 100 Then

    MsgBox ("Lütfen 0 - 100 arasında bir sayı giriniz")

    GoTo b

Else

c:

c = InputBox("Final notunu giriniz")

    If c < 0 Or c > 100 Then

    MsgBox ("Lütfen 0 - 100 arasında bir sayı giriniz")

    GoTo c

Else

d = ((a + b) / 2) * 0.4

e = (c * 0.6) + d

If e > 50 Then

MsgBox ("Geçtiniz")

FontBold = True

FontSize = 12

FontName = "tahoma"

ForeColor = RGB(15, 65, 85)

Print "Ortalama Notunuz  =" & e

Else

 

' her if muhakkak end if le kapatılmalı iç içe çok sayıda if konabilir. if tek satırda bitirilebilir.

 

MsgBox ("Kaldınız")

f:

    f = InputBox("Bütünleme notunu giriniz")

    If f < 0 Or f > 100 Then

    MsgBox ("Lütfen 0 - 100 arasında bir sayı giriniz")

    GoTo f

Else

        but = (f * 0.6) + d

        If but > 50 Then

        MsgBox ("geçtiniz")

        Else

        MsgBox ("artık sizi kimse kurtaramaz")

        End If

        Print "Son notunuz   ="; but

End If

End If

End If

End If

End If

 

ÖRNEK 6

 

Private Sub Command1_Click()

 

'or ve and kullanma

 

Dim a As Integer

Dim b As Integer

Dim c As Integer

Dim d As Double

Dim e As Double

Dim f As Double

Dim but As Double

 

a = InputBox("Birinci vize notu giriniz")

b = InputBox("ikinci vize notu giriniz")

c = InputBox("Final notunu giriniz")

 

d = ((a + b) / 2) * 0.4

e = (c * 0.6) + d

   

If e < 50 Then

MsgBox ("Kaldınız")

 

    ElseIf e >= 50 And e < 65 Then

    MsgBox ("Birinci gruptasınız")

   

    ElseIf e >= 65 And e < 80 Then

    MsgBox ("ikinci gruptasınız")

       

    ElseIf e >= 70 And e < 100 Then

    MsgBox ("Ben birtaneyim")

   

End If

Print "Son notunuz   ="; e

End Sub

 
Saat
 
 
Bugün 3 ziyaretçi (8 klik) kişi burdaydı!
Bu web sitesi ücretsiz olarak Bedava-Sitem.com ile oluşturulmuştur. Siz de kendi web sitenizi kurmak ister misiniz?
Ücretsiz kaydol