Types of Operators

There are different types of operators and they each serve a specific purpose.

1. The assignment ( = ) operator is the is the most obvious and is simply used for assigning a value to a variable or property.
2. The arithmetic operators are all used to calculate a numeric value, and are normally used in conjunction with the assignment operator and / or one of the comparison operators.
3. The concatenation operators are used to concatenate ("Join together") expressions.
4. The comparison operators are used for comparing variables and expressions against other variables, constants, or expressions.
5. The logical operators are used for comparing variables and expressions against other variables; all logical operators can also be used as bitwise operators.
6. The bitwise operators are used for comparing binary values bit-by-bit; all bitwise operators can also be used as logical operators.

First
Next

Operators

An operators acts on one or more operands when comparing, assigning, concatenating, calculating, and performing logical operations.

Say, you want to calculate the difference between two variables A and B and save the result in variable C. These variables are the operands and to find the difference you use the subtraction operator like this:

C = A - B

Here I used the assignment operator ( = ) to assign the difference between A and B, which was found by using the subtraction operator ( - ).

Operators are one of the single-most important parts of any programming language. Without them, you would not be able to assign values to variables or perform calculations and comparisons! It would be a bit like a bicycle without pedals ...

Types of Operators
Operator Precedence

For...Next Loop

Now we'll take a quick look at another kind of loop: the For...Next loop. In this kind of loop, we don't need to worry about infinite loops. This is because the loop is predefined to only execute a certain number of times. Here's a simple example:

Dim Index
MsgBox "Let's count to five. Ready?"
For Index = 1 to 5
MsgBox Index
Next
MsgBox "Wasn't that fun?"

The beginning loop block is defined by the For statement, and the end is defined by the Next statement. This loop will go around exactly five times. The line For Index = 1 to 5 essentially tells the script engine, "Execute this block of code as many times as it takes to count from 1 to 5, and use the Index variable to keep track of your counting. When we've gone through this code five times, stop looping and move on." Notice that every time the loop goes around (including the first time through), Index equal 1, the second time through it equals 2, and so on up to 5. It's important to note that after the loop is finished, the value of the Index variable will be 6, one number higher than the highest value in our For statement. This occurs because the Index variable is incremented at the end of the loop, after which the For statement tests the value of Index to see if it is necessary to loop again.

It's difficult to express the real-world usefulness of the For...Next loop without opening a can of worms on a lot of other subjects, but keep in mind that it is often used to traverse some sort of finite piece of data, such as a word, or a text file. For example, the word "elephant" has exactly eight letters. If you first calculate the number of letters in the word "elephant", you could use that number to drive a For...Next loop. Below is a simple example that uses the VBScript Len ( ) function to calculate the length of the word "elephant." Inside the loop, it uses the Mid ( ) function to pull one letter of the word "elephant" at a time.

Dim Index
Dim WordLength
WordLength = Len("elephant")
For Index = 1 to WordLength
MsgBox Mid("elephant", Index, 1)
Next
MsgBox "elephant"

First

President of Pakistan Pervez Musharraf Resigned

Pervez Musharraf, the President of Pakistan, today on the 18th of August 2008, resigned from his presidency because of the extreme pressure of the fear of impeachment by the Democratic Ruling Elite. He was in a state of fear of being given the capital punishment under the Article-6 of the Constitution of Pakistan. The Pakistan's Democratic Ruling Elite had offered him safe passage as a reward of his resignation. Former President of Pakistan, Pervez Musharraf, ruled over Pakistan for approximately nine years by the use of every illegal mean he could. People of Pakistan are dancing with joy and happiness after that Pakistan got rid of a person who is hated every where in the country because of his cruelty and serial killing.

Loop While in Do...Loop Code

Dim Greeting
Dim UserName
Dim TryAgain
Do
TryAgain = "No"
UserName = "InputBox("Please enter your name:")
If Trim(UserName) = " " Then
MsgBox "You must enter your name."
TryAgain = "Yes"
Else
Greeting = "Hello. " & UserName & ", it's a pleasure to meet you."
End If
Loop While TryAgain = "Yes"
MsgBox Greeting

Do
TryAgain = "No"
UserName = "InputBox("Please enter your name:")
If Trim(UserName) = " " Then
MsgBox "You must enter your name."
TryAgain = "Yes"
Else
Greeting = "Hello. " & UserName & ", it's a pleasure to meet you."
End If
Loop While TryAgain = "Yes"
MsgBox Greeting

Now we encounter the end of our loop block. What this Loop line is essentially telling the script engine is "If the TryAgain variable equals "Yes" at this point, then go back up to Do line and execute all that code over again." If the user entered their name, then the TryAgain variable will be equal to "No". Therefore, the code will not loop again, and will continue on to the last line:MsgBox Greeting, which we've seen before.

If the user did not enter their name, then TryAgain would be equal to "Yes", which would mean that the code would jump back up to the Do line again. This is where the re-initialization of the TryAgain variable to "No" is essential. If we don't reset the value of TryAgain, then there's no way for TryAgain to ever equal anything but "Yes". If TryAgain always equals "Yes", then the loop keeps going around and around forever. This is disaster for your script.=, and for your user.

First
Next

Trim Function in Do...Loop Code

Dim Greeting
Dim UserName
Dim TryAgain
Do
TryAgain = "No"
UserName = "InputBox("Please enter your name:")
If Trim(UserName) = " " Then
MsgBox "You must enter your name."
TryAgain = "Yes"
Else
Greeting = "Hello. " & UserName & ", it's a pleasure to meet you."
End If
Loop While TryAgain = "Yes"
MsgBox Greeting

Do
TryAgain = "No"
UserName = "InputBox("Please enter your name:")
If Trim(UserName) = " " Then
MsgBox "You must enter your name."
TryAgain = "Yes"
Else
Greeting = "Hello. " & UserName & ", it's a pleasure to meet you."
End If

Now we are testing our input. The line IF Trim(UserName) = " " Then tests to see if the user typed in their name. If they typed something in, the code immediately after the Else line will execute. If they did not (or if they clicked the Cancel button), then the UserName variable will be empty, and the code after the If line will execute instead. If the user did not type in their name, we display a message informating them that they have done something our script did not like. Then we set the TryAgain variable (our "control" variable) to "Yes". This ensures that the loop will go around again, and then we'll ask the user for their name again.

If the user did type their name, then we initialize our familiar Greeting variable. Note that in this case, we do not change the value of the TryAgain variable. This is because there is no need to loop around again - the user has obliged us and entered their name. The value of TryAgain is already equal to "No", so there's no need to change it.

First
Next

Input in Do...Loop Code

Dim Greeting
Dim UserName
Dim TryAgain
Do
TryAgain = "No"
UserName = "InputBox("Please enter your name:")
If Trim(UserName) = " " Then
MsgBox "You must enter your name."
TryAgain = "Yes"
Else
Greeting = "Hello. " & UserName & ", it's a pleasure to meet you."
End If
Loop While TryAgain = "Yes"
MsgBox Greeting

Do
TryAgain = "No"
UserName = "InputBox("Please enter your name:")

This should look familiar. We are using the InputBox function to ask the user for their name. We store the return value from the function in the UserName variable. Whatever the user types in, if anything, will be stored in this variable. Put another way, our script is receiving some external input - and remember that I said input is always unpredictable.

First
Next

Control Variable in Do...Loop Code

Dim Greeting
Dim UserName
Dim TryAgain
Do
TryAgain = "No"
UserName = "InputBox("Please enter your name:")
If Trim(UserName) = " " Then
MsgBox "You must enter your name."
TryAgain = "Yes"
Else
Greeting = "Hello. " & UserName & ", it's a pleasure to meet you."
End If
Loop While TryAgain = "Yes"
MsgBox Greeting

Do
TryAgain = "No"

Here we are initializing our "control" variable . We call it the "control" variable because this variable will ultimately control whether or not the loop loops around again. We want to initialize this variable to "No" so that, by default, the loop will not loop around again. Only if a certain something occurs inside the loop will we set TryAgain to "Yes". This is yet another strategy in our ever-vigilant desire to expect the unexpected.

First
Next

Do in Do...Loop Code

Dim Greeting
Dim UserName
Dim TryAgain
Do
TryAgain = "No"
UserName = "InputBox("Please enter your name:")
If Trim(UserName) = " " Then
MsgBox "You must enter your name."
TryAgain = "Yes"
Else
Greeting = "Hello. " & UserName & ", it's a pleasure to meet you."
End If
Loop While TryAgain = "Yes"
MsgBox Greeting

Do

This starts the loop. This tells the script engine that we are starting a block of code that will define a loop. The script engine will expect to find a Loop statement somewhere further down in the script. This is similar to the If...End If code block: the script engine expects the block to be defined with beginning and ending statements. I'll get more into this later but take a note the Do statement on a line all by itself means that the loop will execute at least once. Even if the Loop While statement at the end of the block does not result in a loop-around back to the Do line, the code inside this block is going to execute at least one time.

First
Next

Controlling Loop in VBScript

Dim Greeting
Dim UserName
Dim TryAgain
Do
TryAgain = "No"
UserName = "InputBox("Please enter your name:")
If Trim(UserName) = " " Then
MsgBox "You must enter your name."
TryAgain = "Yes"
Else
Greeting = "Hello. " & UserName & ", it's a pleasure to meet you."
End If
Loop While TryAgain = "Yes"
MsgBox Greeting

We are using the TryAgain variable to control the loop. The loop starts at the word Do. At the End of the loop, if the TryAgaon variable equals "Yes", then the code starting at the word Do will execute again. Notice that at the top of the loop we initialize the TryAgain variable to "No". It is essential that this initialization take place inside the loop (that is, between the Do and Loop statements). This way the variable is re-initialized every time a loop occurs. If we did not do this, we would end up with what's called an infinite loop.

Infinite loops are undesirable, and whenever you code any kind of loop, you need to take measures to make sure they do not produce an infinite loop. As the term suggests, an infinite loop is one that never stops. Remember that the computer will only do what your script tells it to do. If you tell it to keep looking forever, it will. As long as the loop keeps looping, the code that comes after the loop never gets executed.

Let us move on to a line by line explanation of the code.

First
Next