If...Then...Else...End If Statement in VB

Thursday, August 13, 2009 ·

If..Then..Else..End If statement is one of conditional statements in Visual Basic 6 Programming. The statement format is as the following:

  • For 2 conditions
    If Condition1 Then
    Statement1
    Else
    Statement2
    End If
    Statement1 will be executed if Condition1 met (the condition1 result is true value), otherwise the Statement2 will be executed.

  • For 3 conditions or more

    If Condition1 Then
    Statement1
    Else
    If Condition2 Then
    Statement2
    Else
    If Condition3 Then
    Statement3
    Else
    Statement4
    End If
    End If
    End If
    If Condition1 met, the Statement1 will be executed, if Condition2 met, the Statement2 will be executed, and so on.
    Note: that Statement1, Statement2 and so on may be a single statement or a block of statements (one or more statements).

    Example implementation on your program as follow:
  • For 2 conditions
    In an examination if the student get mark more than or equal to 6 he/she pass the exam, however if his/her mark is less than 6 he/she fail.
    So the program coding will be like this:
    If Val(txtMark.Text) >= 6 Then
    lblResult.Caption = "PASS"
    Else
    lblResult.Caption = "FAIL"
    End If
    Look at below image for your illustration:
    Sample Program If Then Else End If Statement VB6

  • For 3 conditions or more
    In an examination the mark will be categorized as below conditions:
    Mark = 10 is Excellent
    Mark >= 9 and <10 is Very Good
    Mark >= 8 and < 9 is Good
    Mark < 8 is Poor
    So the program coding will look like below list:
       If Val(txtMark.Text) < 8 Then
    lblCategory.Caption = "POOR"
    Else
    If Val(txtMark.Text) >= 8 And Val(txtMark.Text) < 9 Then
    lblCategory.Caption = "GOOD"
    Else
    If Val(txtMark.Text) >= 9 And Val(txtMark.Text) < 10 Then
    lblCategory.Caption = "VERY GOOD"
    Else
    lblCategory.Caption = "EXCELLENT"
    End If
    End If
    End If
    Look at below image for your illustration:
    Sample VB Program If Then Else End If Statement


  • 0 comments:

    Post a Comment