asp-5-1 asp control structures colorado technical university it420 tim peterson

25
ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

Upload: maryann-holt

Post on 05-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-1

ASP Control Structures

Colorado Technical University

IT420

Tim Peterson

Page 2: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-2

Definitions

• Flow - Order of execution of statements.

• Execution - Process of carrying out the instruction in a statement.

• Action Statement - Statements that perform an activity.

• Control Statement - Statements that provide instructions on which statements to execute.

• Code Structures - Several lines of a code that achieve a task.

• Control Structures - a set of statements that govern the order of execution.

Page 3: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-3

Control Statements

• Three types of control statements exist:– Branching Structures - Perform a test

• If..Then..Else

• Select Case

– Looping Controls - allow same block of code to run repetitively.

• For Next

• Do While

– Jumping Controls - allows the programmer to pass execution to another block of code.

• Subprocedure

• Function

Page 4: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-4

If Then Else

• If Then Else can consists of four parts:– An expression used to test for a true false

answer– “if true” section of code– “if false” section of code– Ending statement

Page 5: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-5

If Then Else Examples

• If Then Else can be built four different ways:– 1st method - Used if you only have one

statement to perform in case of a “true” test<%

If varFaxConfirm = “Yes” then Response.Write “Please enter your fax number”

%>

Page 6: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-6

If Then Else Examples - Cont’d

– 2nd method - Used if you have more than one statement to execute in the case of a “true” test

<%

If varFaxConfirm =“Yes” Then

Response.Write “Please click below and provide your fax number.”

Response.Write “<A HREF=http://www.ctu.com/FaxForm>Click here</A>”

End If

%>

Page 7: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-7

If Then Else Examples - Cont’d

– 3rd method - Used if you want to perform one state in the case of a “true” and a “false” test.

<%

If varFaxConfirm =“Yes” Then

Response.Write “Please enter your fax number.”

Else

Response.Write “No fax confirmation will be sent.”

End If

%>

Page 8: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-8

If Then Else Examples - Cont’d

– 4th method - Used to continually nest statements using ElseIf

<%

If varConfirm = “Fax” then

Response.Write “Please enter your fax number.”

ElseIf varConfirm = “Email” then

Response.Write “Please enter your email address.”

ElseIf varConfirm = “Voicemail” then

Response.Write “Please enter your voice mail number.”

Else

Response.Write “No confirmation will be sent.”

End If

%>

Page 9: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-9

If Then Else Common Errors

• Devising a test that does not resolve to true or false

• Leaving out the End If

• Leaving out the Else

• Coding End If as EndIf

• Multiple tests can only be accomplished with the ElseIf.

Page 10: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-10

Select Case

• Select Case consists of four parts:– State which variable to test– State a possible answer and what to do if the

answer is a match– Repeat for all possible answers that are to be

handled– End the Select Case Structure

Page 11: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-11

Select Case Examples

– 1st example - This is the simplest method:<%

Select Case varConfirmation

Case “Fax”

Response.Write “<A HREF = ‘FaxConfirmation.asp’ >Fax</A>

Case “Telephone”

Response.Write “<A HREF = ‘Telephone.asp’>Telephone</A>

Case “Email”

Response.Write “<A HREF = “Email.asp’>Email</A>

End Select

%>

Page 12: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-12

Select Case Examples - Cont’d

– 2nd example - Illustrates how to process data that does not match any selection criteria.

<%

Select Case varMonthPref

Case “march”

Response.Write “Your meeting will be held on March 15th “

Case “March”

Response.Write “Your meeting will be held on March 15th “

Case “april”

Response.Write “Your meeting will be held on April 16th “

Case “April”

Response.Write “Your meeting will be held on April 16th “

Case Else

Response.Write “your request for “ & varMonthPref

Response.Write “is not recognized. Please click the back button on your browser “

Response.Write “and reset then re-enter the form data again. <BR>”

End Select

%>

Page 13: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-13

Select Case Examples - Cont’d

• 3rd Example - Multiple selection criteria can be used for one Case statement.

<%

Select Case varMonthPref

Case “march”, “March”, “mar”, “Mar”, “MAR”

Response.Write “Your meeting will be held on March 15th “

Case “april”, “April”, “apr”, “Apr”, “APR”

Response.Write “Your meeting will be held on April 16th “

Case Else

Response.Write “your request for “ & varMonthPref

Response.Write “is not recognized. Please click the back button on your browser “

Response.Write “an reset then re-enter the form data again. <BR>”

End Select

%>

Page 14: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-14

Select Case Examples - Cont’d

• 4th Example - Lets fix the case problem.<%

Select Case Lcase(varMonthPref)

Case “march”, “mar”

Response.Write “Your meeting will be held on March 15th “

Case “april”, “apr”

Response.Write “Your meeting will be held on April 16th “

Case Else

Response.Write “your request for “ & varMonthPref

Response.Write “is not recognized. Please click the back button on your browser “

Response.Write “an reset then re-enter the form data again. <BR>”

End Select

%>

Page 15: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-15

Common Select Case Errors

• Putting more than one variable on the Select..Case line.

• Not making answers mutually exclusive.• Not have a possible answer for each case line.• Typing Select Case or End Select as one word.• Finishing the structure with End Case instead of

End Select

Page 16: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-16

For Next

• For Next statement consists of three parts:– First line describes how many times to go

through the loop.– 2nd- A set of lines that define the action

statements to carry out within the loop.– 3rd - Final line indicates the end of the action

statements and tells ASP to go back and start the loop over.

Page 17: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-17

For Next Example

<HTML>

<HEAD>

<TITLE>ForNext One Response</TITLE>

</HEAD>

<BODY>

<H2>Weekly Client Contacts for George Washington</H2><BR>

<%

varStart=CDate(Request.Form("start"))

varEnd=CDate(Request.Form("end"))

varNumberDays=(varEnd-varStart)

For varLineCounter = 0 to varNumberDays

Response.Write "Clients: ___________________"

Response.Write "<BR ><BR><BR>"

Next

%>

signed _________________________

George Washington

</BODY>

</HTML>

Page 18: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-18

Common For..Next Errors

• Leaving out the counter variable

• Forgetting “=“ or the “to” on the first line

• Leaving out the Next statement

• Forgetting the start or end number

Page 19: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-19

For Each..Next

• This is used for array elements and ASP objects.<%

Dim Item

Dim strCities(1)

strCities(0) = “Colorado Springs”

strCities(1) = “Denver”

For Each Item in strCities

Response.Write Item & “<BR>”

Next

%>

Page 20: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-20

Do While Example

<HTML>

<HEAD>

<TITLE>Do While</TITLE>

</HEAD>

<BODY>

<H1>Sales Calls for This Month</H1><BR>

<%

varRowCount = 1

varTodayDate = day(now())

Do While varRowCount <= varTodayDate

Response.Write "For ____________ "

Response.Write "number of clients met was ______<BR>"

varRowCount = varRowCount + 1

Loop

%>

</BODY>

</HTML>

Page 21: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-21

Common Do..While Errors

• Using Next instead of Loop for closing statement

• Not changing the expression variable (infinite loops)

• Performing one too many or few cycles.

• Using < or >for a test condition when <= or >= should have been used.

Page 22: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-22

DO Loop..While

• This is a variation and forces the code to be executed once before an evaluation is accomplished:

Do

.. Place code here.

Loop While varRowCount <= varTodayDate

Page 23: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-23

Jumping Structures

• Allow you to reuse code.• Subprocedures are used to carry out an action .e.g

putting text on a page.• Functions carry out action statements and return a

result.• Procedures require the following:

– Name

– Code in the body of the procedure

– Ending statement

Page 24: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-24

Subprocedure Examples

Sub SalesContactInfo

Response.Write “Price quotes can be obtained from”

Response.Write “Tom at 719-555-1234.<BR>”

End Sub

many lines about gears

Call SalesContactInfo

many lines about bolts

Call SalesContactInfo

Page 25: ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson

ASP-5-25

Functions

• Written similar to procedures• 1st line of a function starts with “Function”• Last line of a function is “End Function”• Function usually receive a parameter

– Function SimpleDollar(varMoney)

• The function name becomes the variable passed back out of the function “SimpleDollar”

• Functions are called by just typing its name– varMyMoney = SimpleDollar(BigMoney)