If…Then Select…Case For…Next For Each….Next
Do…While While…Wend Functions
& Subroutine
Exit

Asp Control statements
are statements that control the execution order.In asp there are three types of control
statements. Conditional statements, Looping control , and jumping control.

Asp Conditional statements

1) If….Then statement

Asp if statement
is used to help a programmer determine whether to execute one set of code or
another. The syntax for an if statement is the following:

<%

If (expression) Then


    ‘some code


Else

    ‘some other code


End If

%>

Remember the following
points while using If…Then

  • The result of
    expression must be True or False.
  • WE can evaluate
    only one expression in an If..Then structure.
  • If there are
    more than on line of action statements then you must use the End if .
  • We can use nested
    if statements to check multiple conditions. Just remember to include and “End
    If” with each   condition you test for. If you forget to include this
    the ASP Runtime will give you an error.


Eg:
<%

If Num = 100 Then

    Response.Write(“Cograt!!
You got matching number”)

Else

    Response.Write(“Sorry!!.Try
Again”)

End If

%>

Top

2) Select Case statement

Select statements
are useful when you want to test one condition and need to make a choice among
several answers. The syntax is:

<%

Select Case num

    Case “1”



Response.Write(“num=1”)

    Case “2”



Response.Write(“num=2”)

    Case “3”



Response.Write(“num=3”)

    Case “4”



Response.Write(“num=4”)

    Case Else


    Response.Write(“num equals
something else…”)

End Select

%>

 The first
line states that you want compare the values to variable ‘num’.Then ASP test
the contents of variable against the values shown in the ‘case’ line.If it finds
a match it will execute the code upto the next case line and then jumpdown to
first line after End select.Remember that it is case sensitive , ie. if we are
looking for  a variable ‘FL’  it is not same as ‘Fl’. The following
example gives an improved method of case select.

<%

Select Case strstate


    Case “FL” , “Fl” ,”fl”



Response.Write(“Your state is Florida”)

    Case “NJ” , “Nj” ,”nj”



Response.Write(“Your state is NewJersey”)

    Case “NY” ,”Ny” , “ny”



Response.Write(“Your state is NewYork”)

    Case “CA” ,”Ca”,”ca”



Response.Write(“Your state is California”)

    Case Else


    Response.Write(“Your
state not listed here.”)

End Select

%>

Top



Asp Looping Control

1) For…Next Statement

The syntax is:

<%

For Count = StartCount to EndCount


    ———-


    ———-


Next

%>

The for next staement has three
parts.The first line describes how many times to repeat the loop.Second is a
set of lines with action statements that carry out the task you want to repeat.Last
line is end action statement and tells ASP to go back and repeat action statements
again.

Eg:

<%

For i = 10 to 14

response.write(“This is ” & i &”
th line<hr>”

next

%>

Top


2) For Each…Next statement

The
FEN loop works identical to For..Next statement except that it is used to loop
through collections like recordsets, form collections, or arrays.In this case
we don’t have to specify the number of times we want to loop.

Eg:

<%

Dim strfrutes(3)

strfrutes(0) = “grapes”


strfrutes(1) = “oranges”


strfrutes(2) = “apples”


strfrutes(3) = “mangos”

For
each ft In strfrutes

  tesponse.write ft


Next

%>

Top


3)Do…While staement

It
is used to repeat the loop while specified condition exist. The difference between
For Loop and Do Loop is that

the For Loop did a specific action
a certain number of times where as the Do Loop continues to do a specific action
until the supplied statement is found to be true.

Eg:

<%

i=10

Do While i<14

response.write(“This is ” & i &
“th line.<hr>”)

i=i+1

Loop

%>

Top


4)While…Wend Statement
  The
While…WEnd loop is very similar to the Do loop. This loop performs the
requested action until a condition is met. The only difference is a small amount
of syntax.


<%

    While {Condition}


     ……..


    Wend


    %>

The
While loop is commonly used to loop through a recordset that has been returned
from a database. That we will se in later portion.

Top



Jumping Control

Asp Functions And Subroutiens

In Asp function is a
group of lines of code in a program that performs a specific task . A Sub is
a group of lines of code in a program that performs a specific task. A function
returns a value and a sub does not return a value.

Asp Functions

<%

Function functionName(parameter)


   ……………


   Some code


  …………


End Function

%>

To use the function
in the main progrm just type the named function followed by the parentheses.

Top




 Asp Exit statement

 Using the
Exit statement you can force the flow of your program to exit the current set
of logic it is in. An example of when you might want to do this is when you
have a few nested if statements. Once you are in the if statements you check
a certain condition and find it to be false. At this point you want to flow
of your program to exit out of the current sub routine. With the Exit statement
you can do this.

Eg:
<%

i=10

Do While i<14

If i = 13 Then

 Response.Write(“Exiting Do loop…”)


Exit Do

End If

response.write(“This is ” & i &
“th line.<hr>”)

i=i+1

Loop

Result:
This code will loop through until i equals 13, then it will print “Exiting Do
loop…”

and
exit the loop.

Top


Leave a Comment