Sunday, February 23, 2020

VBScript tutorial 3 | If Statement | Conditional Statements

This is the third tutorial in the VBScript Tutorials for Beginners. This VBScipt beginner tutorial explains VBScript conditional statements. Please view the VBScript tutorial 3 or read on... A VBScript conditional statement has a condition. It runs one code block if the condition is True or another code block if the condition is False. A conditional statement in VBScript is the If Then statement. The example of a VBScript If Then statement is as follows:

' VBScript code
Option Explicit
Dim name
name = InputBox("Please enter a name", "Name Entry Dialog", "enter name here")
' If Then statement can run only one statement (e.g. the MsgBox statement) if the condition is True
' No statement is run if the condition is False
If Len(name)>5 Then MsgBox "The name is " & name & " and it's length is " & Len(name) & " letters."
' We can execute this VBScript as explained in my VBScript tutorial 1.

Next, let us learn about the VBScript If Then EndIf statement. It can run a code block (multiple statements) if the condition is True. No statement is run if the condition is False. The above VBScript example can be modified using the If Then EndIf statement as follows:

' VBScript code
Option Explicit
Dim name
' The VBScript Trim function removes the spaces on either side of a string.
name = Trim(InputBox("Please enter a name", "Name Entry Dialog", "enter name here"))
If Len(name)>5 Then
    ' This code block has two VBScript statements (MsgBox statements).
    ' The VBScript Ucase function converts the name argument to UPPER case.
    MsgBox "The name is " & UCase(name)
    MsgBox " The length of the name is " & Len(name) & " letters."
End If

Now, we may want to run one code block if a condition is True and another code block if that condition is False. A conditional statement in VBScript is the If Then Else EndIf statement. The example of a VBScript If Then Else EndIf statement is as follows:

' VBScript code
Option Explicit
Dim strTime
REM Comments can start with the REM statement.
REM Time function gives the current time.
strTime = Time
REM InStr function finds the string "AM" within strTime's value from the 1st character position.
If InStr(1, strTime, "AM")<>0 Then
    REM If code block starts
    MsgBox "The current time is " & strTime & ". Have a good morning!"
    REM If code block ends
Else
    REM Else code block starts
    MsgBox "The current time is " & strTime & ". Have a good day and good night!"
    REM Else code block ends
End If

The above VBScript If statements are useful when we need to test a single condition. But, we may need to test multiple conditions and run different code blocks accordingly. Another conditional statement in VBScript is the If Then ElseIf EndIf statement. The example of a VBScript If Then ElseIf EndIf statement is as follows:
' VBScript code
Option Explicit
'  intDayOfWeek will contain an integer value.
Dim intDayOfWeek
' Date function returns the current date
' WeekDay function returns the day of the week, 1 for Sunday through 7 for Saturday
intDayOfWeek = WeekDay(Date)
' strMessage will contain a message (string) for the user.
Dim strMessage
If intDayOfWeek = 1 Then 'Instead of 1, we may write vbSunday, which is more readable.
    strMessage = "Sunday"
ElseIf intDayOfWeek = 2 Then 'Instead of 2, we may write vbMonday and so on.
    strMessage = "Monday"
ElseIf intDayOfWeek =  3 Then
    strMessage = "Tuesday"
ElseIf intDayOfWeek = 4 Then
    strMessage = "Wednesday"
ElseIf intDayOfWeek = 5 Then
    strMessage = "Thursday"
ElseIf intDayOfWeek = 6 Then
    strMessage = "Friday"
ElseIf intDayOfWeek = 7 Then
    strMessage = "Saturday"
Else
    strMessage = "Unknown day"
End If
MsgBox "Have a happy " & strMessage

Another conditional statement in VBScript is the nested If statement. When a VBScript If statement is within another If statement, it is the nested If statement. The example of a nested VBScript If is as follows:

' VBScript code
Option Explicit
Dim a, b, c, biggest
a = 100
b = 150
c = 900
If a > b Then 'Outer If code block begins
    If a > c Then
        biggest = a
    Else
        biggest = c
    End If 'Outer If block ends
Else 'Outer Else block begins
    If b > c Then
        biggest = b
    Else
        biggest = c
    End If
End If 'Outer Else block ends
MsgBox "The biggest number is " & biggest

Want to understand these VBScript examples in detail? Please view my VBScript tutorial 3. Thank you.

1 comment:

  1. Great blog on software testing. Hope to read more blogs from you.

    ReplyDelete