Tuesday, April 5, 2011

Control Structures (Loops and Branches)


Branches


If


The "if" statement is a fundamental control statement. It allows your program to perform a test, and act based on the results of that test. For example:


if ( (x = 1) and (y = 3) ) then
sum = y - x
end if


In this statement the value of the x variable is compared to 1 to see if it is equal,and the value of the y variable is compared with 3 to see if it is equal. The use of the “and” operator, adds an additional logical condition that says that the first comparison must be true and the second comparison must be true for the overall result of the test to be true. If the test results in an overall true condition then the statements that follow the if statement will be executed. If the test results are false nothing will occur.


An additional clause you can add to the "if" statement is the "else", an example:


if (sum = 0) then
sum = x + y
else
subtotal = sum
end if


This statement is read as: if sum equals 0 then sum equals x plus y, or else
subtotal equals sum.


ElseIf


ElseIf can be used to refine your code so that you can reduce the number of
statements and make your program code easier to read.


If (sum > 10) then
Alert( “Sum is greater than ten”)
ElseIf (sum > 5 ) then
Alert( “Sum is greater than five”)
End If


If (sum > 10) then
Alert( “Sum is greater than ten”)
ElseIf (sum > 5 ) then
Alert( “Sum is greater than five”)


Nested Ifs


You can also nest your If statements. Nesting is where one statement is
contained within another one. You would do this when you need to meet one
condition before you test for the second one.


If (x>10) then
If (y>5) then
Rem do something
ElseIf (y>10) then
Rem do something else
End if
End if


Select Case


The select case statement is handy when a variable may take on a number of
values and you want to test for some of those values. The use of “select case” is shorter and easier to read than a number of "if" statements.


Select case n
case 1
REM start here if n equals 1.
REM place code here
case 2
REM start here if n equals 2.
REM place code here
case 3
REM start here if n equals 3.
REM place code here
Case else
REM if n is not equal to 1, 2 or 3
REM case else is optional
End Select
A case can also take multiple values:
Case 5, 10, 15
A case can also take strings instead of numeric values:
Case “red


Loops


A loop is a programming structure that forces the statements contained within its delimiters to execute over and over again until a condition is met at which point the loop ends.


Do while … Loop and do until … Loop


While a condition is true, execute one or more statements. “While loops” are
especially useful when you do not know how many times you have to loop, but
you know you should stop when you meet the condition.



dim x
x=1
do while x <= 10 : REM loop until x is greater than 10
until x is greaterthan 10
x = x + 1 : REM add one to the value of x
loop
x=1
do until x = 10 : REM loop until x is greater than 10
until x is greaterthan 10
x = x + 1 : REM add one to the value of x
loop
Loops that execute at least once.
x=1
do
until x is greater than 10
      x = x + 1 : REM add one to the value of x
loop until x = 10 : REM loop until x is greater than 10


For … Next


"For loops” are useful when you know exactly how many times you want the loop
to execute.
for x = 1 to 10 : REM loop while x is <= 10
do something ten times
Next
The loops normally increment by 1, you can alter increment using the ""step”
statement. You can change the step to be positive or negative.
i.e. for x = 0 to 10 step 2
i.e. for x = 10 to 0 step –2
Remember that your step value should increment and decrement to the final
value.
Exit For
You can also exit out of a for loop before you have reached the end of the loop.
The “Exit For” statement can be used with a condition, like below.
for x = 1 to 10 : REM loop while x is <= 10
REM do something ten times
If (sum>10) then
       Exit For
End if
Next
Learning VBScript
24

For Each … Next


The For Each … Next statement makes working with collections easier. An easy
way to think about it is; loop through all the items in a collection. It is exceptionally handy because you don’t need to determine the beginning and
ending points of the collection to construct your loop. In the example below rs is the object and fields is a collection contained in that object. We will discuss
collections in detail when we cover objects.


Dim customer_field
For Each customer_field in rs.fields
      Rem Display the field
Next


While … Wend


While … Wend is an older loop construct. Microsoft recommends using the Do …
Loop because it affords more flexibility.


Dim X
X=1
While ( X < 10 )
Rem do something while C < 10
X=X+1
Wend

No comments:

Post a Comment