Wednesday, April 14, 2010

Write a VB Script to Print different Operations using Select Statement

Click on Back to the Assignment Question Page

This Code gives an idea as to how we can use FOR loop in our script to make it more meaningful and reduce the no of lines of code.


Hope you remember the SELECT CASE statement in which we have looked at a code where in the user can perform a particular type of operation like addition, subtraction, multiplication and division. Now using the For loop try to ask the user how many times the operation should perform. For ex if the user enters 3 then the code should run for three times.

Look the script for this Select Case Statement

The script for this is given below. What i have done is that before the user to select the operation before this line we will add few code like an inputbox and a for loop. The code to be added in the top is like this and at the end of the code add NEXT. Now we are asking the user how many times the user wants to perform the operations and taking that value in the variable Varint1 and converting it into integer. Remember that by default a variant is a string. Then we are passing the value into the for Loop and incrementing by one value.

Varint1 = CINT(Inputbox("Enter the number of times this operation to be performed"))
For i = 1 to Varint1 Step 1
Look at the entire code
Varint1 = CINT(Inputbox("Enter the number of times this operation to be performed"))
For i = 1 to Varint1 Step 1
UserInput = Inputbox("Enter any of the following options"&vbnewline&" " &vbnewline&"1 for Addition"&vbnewline&"2 for Subtraction"&vbnewline&"3 for Multiplication"&vbnewline&"4 for Division")
Var1 = CINT(Inputbox("Enter the First Value"))
Var2 = CINT(Inputbox("Enter the Second Value"))
aaaSelect Case UserInput
aaaCase 1
aaamsgbox"The Sumation of "&Var1& " and " &var2& " is: "&Var1 + Var2
aaaCase 2
aaamsgbox"The difference between "&Var1& " and " &var2& " is: "&Var1 - Var2
aaaCase 3
aaamsgbox"The product of "&Var1& " and " &var2& " is: "&Var1 * Var2
aaaCase 4
aaamsgbox"The quotient between "&Var1& " and " &var2& " is: "&Var1 \ Var2
aaaCase Else
aaamsgbox "Invalid entry"
aaaEnd Select
Next
Click on Back to the Assignment Question Page

1 comment:

abc said...

Hi,
how can we make a infinite loop, meaning like till user wants to perform operation.

Post a Comment