Showing posts with label Switch Case. Show all posts
Showing posts with label Switch Case. Show all posts

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

VB Scripting - Looping Constructs

Previous11121314151617181920Next

Different types of Looping Constructs in VB Script and its usage in VB Scripting.


Looping constructs allows a user to run a group of statements or piece of code repeatedly that many times as desired by the user. There are some loops which repeat statements until the condition become false and others loops which repeat statements until the condition becomes true. Also there are those loops which repeat statements for a specific number of times.

There are different kinds of looping construct and based on the requirements we can use any of this Looping constructs. The are basically two types of looping constructs FOR and DO. Let’s look at them

FOR Looping Construct

1. For Next Loop
2. For Step Next Loop
3. For Each Next


DO looping Constructs

1. Do Loop While
2. Do Loop Until
3. Do While Loop
4. Do Until Loop
5. While WEnd Loop


Previous11121314151617181920Next

VB Scripting - Constants - Built In Constants

Previous11121314151617181920Next


What is a Built in Constant in VB script?

Built in Constants are those which are already created and stored in VB Library and they cannot be changed and they can be used as it is for various purposes. They in turn help you to print information with meaning and also messages with some action.



Different types of Built in Constants

Please find the list of built in constant and their meaning.

Built in ConstantsWhat it does
vbOKOnlyOK button only
vbOKCancelOK and Cancel buttons
vbAbortRetryIgnoreAbort, Retry, and Ignore buttons
vbYesNoCancel Yes, No, and Cancel buttons
vbYesNoYes and No buttons
vbRetryCancel Retry and Cancel buttons
vbCritical Critical Message icon
vbQuestion Warning Query icon
vbExclamation Warning Message icon
vbInformation Information Message icon
vbDefaultButton1 First button is default
vbDefaultButton2 Second button is default
vbDefaultButton3 Third button is default
vbDefaultButton4 Fourth button is default
vbApplicationModal Application modal i.e. the current application will not work until the user responds to the message box
vbSystemModal System modal i.e. all applications wont work until the user responds to the message box


Example

Const TEST = "Are you sure you want to exit"
msgbox TEST, vbYesNo

The message box displayed will be like this with Yes and No buttons.



Previous11121314151617181920Next

VB Scripting - Constants

Previous11121314151617181920Next

Constant in VB Scripting is a meaningful and specific name that takes the place of a data type like integer or string and never changes. There are two types of constants.



1. Named Constant
2. Built in Constant

Constant is like a variable but unlike variable the value does not change for the constant. It is a permanent value stored in the memory.

Named Constant

You will actually assign a integer or a string to a variable but the value assigned to this variable cannot be changed. It is like a variable but not behave like a variable. The value does not change and it cannot be used as a variable.

As part of the coding convention it is better to write all the constants in upper case even though VB Scripting is case insensitive but for easy understanding it is written and this makes no difference.

Note: The variables are written in lower case. A constant cannot be used as a variable.

You can create user defined constants using the const statement and can be created on numeric and string values giving meaningful names.

Syntax

Const [Name] = [Expression]


Const GREETING = “Welcome to the world of constants”
Const Age = 50


Example

Const GREETING = “Welcome to VB Scripting”
Msgbox GREETING


Previous11121314151617181920Next

Tuesday, April 13, 2010

VB Scripting - Select Case

Previous11121314151617181920Next

Select Case statement in VB Scripting is also a branching statement. We branch out the code by evaluating one expression. Select Case is an alternative for If Else. Select Case can be used invariably (any where)



Syntax for Select Case Statement


Select Case [condition]
Case 1
aaaCode
Case 2
aaaCode
Case 3
aaaCode
Case Else
aaaCode
End Select


Example to understand it easily


Let us write a script which can display the resultant value of two integers based on the selection made. We are computing Addition, Subtraction, Multiplication and division of the two integers and based on the user selection the operation is performed.

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"))

Select Case UserInput

Case 1
aaamsgbox"The Sumation of "&Var1& " and " &var2& " is: "&Var1 + Var2
Case 2
aaamsgbox"The difference between "&Var1& " and " &var2& " is: "&Var1 - Var2
Case 3
aaamsgbox"The product of "&Var1& " and " &var2& " is: "&Var1 * Var2
Case 4
aaamsgbox"The quotient between "&Var1& " and " &var2& " is: "&Var1 \ Var2
Case Else
aaamsgbox "Invalid entry"
End Select


Previous11121314151617181920Next