VBScript Control Structures

M&A Ch. 4


Control Structures 

  • provide program logic

    • test conditions

    • branch
       


Example:

For iLoop = 1 to 5

response.write "iLoop=" & iLoop "<br>"

Next

 

(Note the typo)


 

Four major types of control structures:

  1. If  then

  2. Do while ... 

  3. For Next

  4. Subroutines & functions

  • Plus many variations
     


1. 

IF condition THEN statement 

  

2. 

IF condition THEN

statements

END IF

 

3.

IF condition THEN

statements

ELSE

statements

END IF

 


Example:

If  hour( time() ) < 12 then

      response.write "Good morning"

else

     response.write "Good afternoon"

End If


 

4.

IF condition1 THEN

statements

ELSEIF condition2 THEN

statements

ELSEIF condition3 THEN

statements

ELSE

statements

END IF

 

5.

DO WHILE condition

statements

LOOP

Most frequent use:

  • Looping through database recordsets
     
  • Often don't know how many records there are
     

 

Caution: 

Dim iLoop

iLoop = 1

Do while iLoop > 0

     response.write "Loop=" & iLoop

     iLoop = iLoop + 1

Loop 

   

  

6.

FOR iCounter = 1 TO n [STEP m]

statements

NEXT


 

Example:

Dim decCounter, decTo, decStep

decTo = Request.querystring("decTo")

decStep = Request.querystring("decStep")

 

For decCounter=1 to decTo STEP decStep

 

      Response.Write "decCounter =" & _

            decCounter & "<br>"

Next

 

Try it:

For decCounter = 1 to step

 



Modifiers

  • NOT

  • AND, OR
     

  • Parentheses () 

 
Example 1:

Do While intYear > 1999 AND _

                            strStudentName > "" 

         Response.write strStudentName & intGradYear

Loop
  

 

Example 2:

If NOT strPassword = "Sandvig" then

     response.cookies("myCookie")("ID") = "OK"

End If

 

 

Example 3:

If strPass = "fire" OR strPass = "water" then

     response.cookies("myCookie")("ID") = "OK"

End If
 

Parentheses
 

  • Use Parentheses to force order of execution
     
  • Improve readability
     
    • 3 * (4 + 2)

     

    • IF NOT true and false and true or false then
       
    • IF NOT (true and false) and (true or false) then
        
       
  • Try to avoid complex logic
      
    • Break down into smaller parts
       


Other ASP Control Structures

 

  • Can create custom:

    • Subroutines

    • Functions

    • Components
       

  • Useful for code used more than once

    • Modularization

    • Encapsulation
       
       

  • Subroutines

    • Perform some action

    • Example & syntax: 

  • Functions

    • Return a result

    • Example:

      • Response.write "Root of 2 = " & Sqr(4)

      

  • Can also use server-side includes
       
    • No encapsulation.
          
    • Use on many pages
       
         
    • Subroutines within SSI
         
       
      • Benefits of SSI & encapsulation


Designing program structure:

  • challenging 

  • creative
     

  • Good structure: 

    • flexible

    • easy to expand

    • easy to modify

    • easy to maintain

    • less prone to logic errors
       

  • Fun part of programming -- when it works.
     

  • Spaghetti Code


Object oriented programming (OOP)
 

  • Each object performs a very specific task
      
  • Mix and match as needed
      
  • Benefits:
     
    • Write once
       
    • Test once
       
    • Reuse many times
        
        
  • ASP provides 7 objects:
       
    • Response, Request, Server...
       
    • Perform complex operations
       
    • May require thousands of lines of code
       
    • We don't need to know how it works
       
    • Fully tested, reuse many times
    • Objects have:
       
       
      • properties
         
      • methods
         
      • collections
         
        MSDN ASP Quick Reference
           

Summary:

  • Control Structures provide program logic
      

  • Subroutines, Functions, 
      

  • Object Oriented Programming
      

  • ASP Objects 
     

Excellent Reference:

Microsoft VBScript Reference

 

Next: VBScripts Built in Functions