Tuesday, April 13, 2010

VB Scripting - Else If Statement

Previous11121314151617181920Next


The If Else statement we discussed previously executes one block of code if the value is greater than 80 or executes the other piece of code if the condition is not satisfied. Now think what you will do if you have different ranges.

To do this you need Else If statement.



What Else if does is that it creates and if statement within an if statement and goes on and in this manner you can check different conditions in a single if Statement.

Syntax for Else If Statement



If [condition] Then
Code
Elseif [condition] Then
Code
Elseif [condition] Then
Code
Elseif [condition] Then
Code
Else
Code
End If


Example to show how Else If Statement is used


Now let write a script to see how this works. We will calculate the average for three subjects and based on the percentage we will display the average and the appropriate messages.


'Calculating the average for three subjects
mt = Inputbox("Enter the marks scored for Manual Testing:")
qtp = Inputbox("Enter the marks scored for QTP:")
sql = Inputbox("Enter the marks scored for SQL:")
'Calculating average
avg=(mt+qtp+sql)/3
'Displaying the average to the user
msgbox "The average marks scored is: "&avg
'Branching statements to display appropriate message
If avg >= 90 Then
msgbox "You scored "&avg&" percentage and you will get a Job"
ElseIf avg >= 80 and avg < 90 Then
msgbox "You scored "&avg&" percentage and you have to put in more effort"
ElseIf avg >= 70 and avg < 80 Then
msgbox "You scored "&avg&" percentage and you are not up to the mark"
Else
msgbox "You scored "&avg&" percentage and you have to repeat the course"
End If



What happened why did this result came as 100.333333333333 and the first message will be displayed which is wrong.

Let’s debug the script line by line.

mt=3, qtp=0, sql=1
avg=(mt+qtp+sql)/3
avg=(3+0+1)/3
avg 301/3
avg=100.333333333333


Let me explain you what happened so that we will be clear as to what happened and how to rectify the code.

Even though we say that in the condition avg=(mt+qtp+sql)/3 we have used addition (+) operator but just to remind that the default type of Inputbox is string because we have not explicitly declared the variables and that’s why by default what ever values you have entered in the input box is taken as string and in context of this + is treated as a concatenation operator which means that it will join the three values as 3+0+1 to make 301 and not add them up to 4. That is the reason we are getting a different result.

In order to rectify this issue we need to use a conversion type (Type Cast) which in our case actually converts the default data type to integer type. For this we need to use CINT. CINT can be used with input box or while on the expression calculating the average (In our case). You cannot use CINT for some variables while accepting values and for some on the expression. You can either use CINT on the expression or while taking input on variables.


Let’s rewrite the code and see what happens

'Calculating the average for three subjects
mt = CINT(Inputbox("Enter the marks scored for Manual Testing:"))
qtp = CINT(Inputbox("Enter the marks scored for QTP:"))
sql = CINT(Inputbox("Enter the marks scored for SQL:"))
'Calculating average
avg=(mt+qtp+sql)/3
'Displaying the average to the user
msgbox "The average marks scored is: "&avg
'Branching statements to display appropriate message
If avg >= 90 Then
msgbox "You scored "&avg&" percentage and you will get a Job"
ElseIf avg >= 80 and avg < 90 Then
msgbox "You scored "&avg&" percentage and you have to put in more effort"
ElseIf avg >= 70 and avg < 80 Then
msgbox "You scored "&avg&" percentage and you are not up to the mark"
Else
msgbox "You scored "&avg&" percentage and you have to repeat the course"
End If




Now run the script and you will get the right percentage and the required message.

Note: You can also use CINT on the expression instead on inputbox. It will look like this.
(CINT(mt)+CINT(qtp)+CINT(sql))\3

Assignments on Else If Statement

1. Write a script to print the greatest of three integers.
2. Write a script to print the least of the three integers.

Click Here for answers for Assignments


Previous11121314151617181920Next

4 comments:

Naveen Kumar S said...

Hi Kiran,

Below is the simple script I am getting wrong output . Please go through the script and assist me where am i going wrong.

Dim mt,sql,qtp,sum

mt = Inputbox ("Enter First number : ")
SQL = Inputbox ("Enter Second number : ")
QTP = Inputbox ("Enter Third number : ")

SUM = mt + sql + qtp

msgbox sum

IF i enter first second and third number as 1 it shows 111

IF i enter first second and third number as 10 it shows 101010

It is not adding the numbers it is just combining and showing Please assist

abc said...

Hi,
Nikhil here

I think naveen u can use CINT function to convert the input to integer. When u are entering they are taken as strings and when u try to add them using + vbscript is appending all 3 inputs.

So ur code can be like
mt = CINT(Inputbox ("Enter First number : "))

Naveen Kumar S said...

Thanks Nikhil, i will try out

Kanikaram Kiranpaul said...

Naveen, when you input the numbers without having CINT then it will take the numbers as strings and not as numeric. So when you use + between the variables vb script identifies them as strings and joins them as strings.

Post a Comment