Showing posts with label VB Script operators. Show all posts
Showing posts with label VB Script operators. Show all posts

Wednesday, August 1, 2012

Arthemetic Operators in VB Script

Following are the different kind of arthemetic operators used in VB Script coding (+, -, *, /, mod, ^, \). They are used in the script to acheive different results on numeric values.

Instead of hardcoding the values in the script the user can either get the input from the user executing the vb script by using inputbox method or can be driven from an excel sheet.

The input of values is again based on the requirement. It gives flexibility for the user if the values are coming from an external source.

a) Addition (+)

Hard Coded Values in the script.

dim x, y
X=1
Y=2
z=x+y
msgbox z
The output would be 3
It should be noted that vb scripting is not case sensitive so the script does not throw error if X or x is used in the script.

Input values from the user for the above script.

dim x, y
X=cint(inputbox("Enter the First Value: "))
y=cint(inputbox("Enter the Second Value to be added with the first value: "))
z=x+y
msgbox "The total of first and second value is: "&z
b) Subtraction (-)

dim x, y
X=cint(inputbox("Enter the First Value: "))
y=cint(inputbox("Enter the Second Value to be subtracted from the first value: "))
z=x-y
msgbox "The final value after subtraction is: "&z
c) Multiplication (*)

dim x, y
X=cint(inputbox("Enter the First Value: "))
y=cint(inputbox("Enter the Second Value to be multiplied with the first value: "))
z=x*y
msgbox "The final value after multiplication is: "
More on Arthemetic operators will be posted in the nex blog.

Wednesday, April 14, 2010

Script to print Prime Numbers

Click on Back to the Assignment Question Page

Write a script to print prime numbers based on the user input for the prime number limit.

n = Inputbox("Please Enter the limit")
For i = 1 to n
aaaCount = 0
aaaa = 1
aaaaaaDo While a<=i
aaaaaaaaaIf i MOD a = 0 Then
aaaaaaaaaaaaCount = Count + 1
aaaaaaaaaEnd If
aaaaaaa=a+1
aaaaaaLoop
aaaaaaaaaIf Count = 2 Then
aaaaaaaaaaaavar = var&" "&i& ", "
aaaaaaaaaEnd If
Next
aaaMsgBox("Prime numbers within " &n&" numbers = :" &var)

Click on Back to the Assignment Question Page

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

VB Scripting - FOR Looping Construct

Previous11121314151617181920Next


FOR Looping Constructs in VB Scripting


For Loop is used to run a piece of code iteratively for a finite number of times. The start and end conditions are known in advance and the counter automatically increments by one and this increment is by default.

FOR Looping Construct Example

Syntax

For [condition]
Code
Next

A simple for loop example

For i = 1 to 10
Msgbox "This loop is repeated "&i& " times"
Next

When you run this script the message box will display “This loop is repeated 1 times” and as you click the number increments till it reaches 10 times and also in the message the number is incremented and displayed.

For Step Next Looping Construct


In this kind of looping construct we actually change the increment to the desired number and the increment happens as we desire. To change the default increment value we use “STEP” along with the For Loop or you can have user defined increment like i=i+1 etc.

Syntax

For [condition] Step [increment/expression]
Code
Next


Example: We will use the above example and change the increment value as desired.

For i = 1 to 10 Step 2
Msgbox "This loop is repeated "&i& " times"
Next

Now when you run the script the value is incremented by 2 and the value in the message box will change and will display as “This loop is repeated 1 times” initially and then the value changes to 3,5,7,9 and the script terminates because next value is 11 and is greater than 10.

Assignment to do for FOR Looping Construct


1. 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

Click Here to view the Answer

2. Write a script to print prime numbers based on the user input for the prime number limit.

Click Here to view the Answer

3. Write a script to generate the Fibonacci series. Again ask the user for the limit.

Click Here to view the Answer

4. Write a script to print all the odd numbers till 100. Do not use the STEP statement.

5. Write a script to convert degrees to Fahrenheit.


Previous11121314151617181920Next