Results 1 to 4 of 4

Thread: Conditional structures for VBScript

  1. #1
    Join Date
    Apr 2010
    Posts
    87

    Conditional structures for VBScript

    Let us now insert some fork in the programs to perform operations under certain conditions the main command is IF. The syntax is as follows:
    Code:
    Then if <condition> 
    <operations if the condition and true> 
    Else 
    <operations if the condition and false> 
    End If
    The following example uses the Month function () which returns the numeric month, and Now () which returns the current date and time:
    Code:
    <script language="VBScript"> 
    <! - 
    if month (Now ()) = 6 then 
    document.write "and June" 
    end if 
    //--> 
    </ Script>
    If this is the month of June, will print "is June". To complete the program:
    Code:
    <script language="VBScript"> 
    <! - 
    if month (Now ()) = 6 then 
    document.write "and June" 
    else 
    document.write "It's June" 
    end if 
    //--> 
    </ Script>
    You can also create structures within other structures or nested structures:
    Code:
    <script language="VBScript"> 
    <! - 
    if month (6 ())<= Now then 
    document.write "We are in the first half of <br> 
    if month (Now ())<= 3 then 
    document.write "and the first quarter." 
    else 
    document.write "and in the second quarter." 
    end if 
    else 
    document.write "We are in the second half of <br> 
    if month (9 ())<= Now then 
    document.write "and in the third quarter." 
    else 
    document.write "and in the fourth quarter." 
    end if 
    end if 
    //--> 
    </ Script>
    Note the importance of a type of formatting "indent" (ie indentations) when using such complex structures.

  2. #2
    Join Date
    Mar 2008
    Posts
    192

    Re: Conditional structures for VBScript

    Another use the IF statement is to give the most possible conditions:
    Code:
    <script language="VBScript"> 
    <! - 
    if month (Now ())<= 3 then 
    document.write "We are in the first quarter." 
    elseif month (6 ())<= Now then 
    document.write "We are in the second quarter." 
    elseif month (9 ())<= Now then 
    document.write "We are in the third quarter." 
    else 
    document.write "We are in the fourth quarter." 
    end if 
    //--> 
    </ Script>
    Note that in this type of structure only a transaction is executed. Starting from the top down, the first true condition is executed the corresponding operation and exit the structure. In fact if we were in January would be all the conditions but would be printed only the first sentence. In the conditions you can use logical operators NOT, AND and OR to combine terms. When using structures with many choices depend on the value of a parameter, you can use the SELECT structure.

  3. #3
    Join Date
    Mar 2008
    Posts
    227

    Re: Conditional structures for VBScript

    That's the same program with the structure of the first SELECT:
    Code:
    <script language="VBScript"> 
    <! - 
    Select Case Month (Now ()) 
    houses 1,2,3 
    document.write "We are in the first quarter." 
    houses 4,5,6 
    document.write "We are in the second quarter." 
    houses 7,8,9 
    document.write "We are in the third quarter." 
    Case Else 
    document.write "We are in the fourth quarter." 
    End Select 
    //--> 
    </ Script>
    In this structure, you define the variable to be controlled with the statement "Select Case Variable", and are then lists the possible values that can take the variable with the statement "case value1, value2, value3" to follow these directions what to do. In the example the variable is numeric, if the string variable the values are put between quotation marks:
    case "value1", "value2", "value3"

  4. #4
    Join Date
    Dec 2008
    Posts
    183

    Re: Conditional structures for VBScript

    You should also know about the cycles which are related to the conditional structures. A cycle is simply to repeat the operations for a number of times, or until a certain condition does not occur. The for loop - NEXT increases with each cycle a variable. When this variable will come to a set value, the cycle will end.
    Code:
    <script language="VBScript"> 
    <! - 
    Option Explicit 
    Dim i 
    For i = 4 to 20 
    document.write i & "<br> 
    next 
    //--> 
    </ Script>
    This program prints the values from 4 to 20 columns. Through the step, I can also count on the contrary, and with several steps:
    Code:
    <script language="VBScript"> 
    <! - 
    Option Explicit 
    Dim i 
    For i = 20 to 4 step -2 
    document.write i & "<br> 
    next 
    //--> 
    </ Script>
    This program will display in numbers 20, 18, 16, 14, 12, 10, 8, 6, 4.

Similar Threads

  1. Advantages and disadvantages of OU structures
    By MrQ89 in forum Active Directory
    Replies: 1
    Last Post: 06-03-2012, 03:31 AM
  2. Replies: 3
    Last Post: 13-10-2011, 02:20 PM
  3. How does a class vary from Structures ?
    By Chellam in forum Software Development
    Replies: 4
    Last Post: 30-12-2010, 08:20 AM
  4. How to use an Alternative Syntax for Control Structures in PHP?
    By - Empty Shell - in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 06:33 AM
  5. Pointer to an array of structures
    By Zool in forum Software Development
    Replies: 3
    Last Post: 13-05-2009, 10:51 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,031,711.20777 seconds with 17 queries