Presentation is loading. Please wait.

Presentation is loading. Please wait.

ASP control structure BRANCHING STATEMENTS

Similar presentations


Presentation on theme: "ASP control structure BRANCHING STATEMENTS"— Presentation transcript:

1 ASP control structure BRANCHING STATEMENTS
Branching statements perform a test. They might check to see if x=2 or y=5 or if x>y. Based upon the results of the test, certain code is executed while other code is skipped. There are two types of branching statements we will run into: If--Then--Else is generally used to choose from one of two sets of lines to execute. EXAMPLE: If intFriends > 5 then      Response. Write "You have a lot of friends" Else      Response. Write "You need to be more social." End If As you can see from above, there are four parts to the If--Then statement. An expression that is either true or false An "if true" section An "if false" section (OPTIONAL) An ending statement

2 Branching Structure Basic If--Then Rules:
Every expression tested will either evaluate to true or false You can only use one statement in the single line if--then If you use more than one line, you must use End If If you want action in the case of a false evaluation, you must use the else statement Once an expression evaluates to true, the If/Then structure is exited. Example

3 Select case SELECT CASE
Although you can use the ElseIf statement to check multiple expressions, you should really use the Select Case branching control structure when that situation arises. Select Case strLanguage      Case "Spanish"           Response.Write "Ola"      Case "English"           Response.Write "Hello"      Case "French"           Response.Write "Bonjour"      Case "German"           Response.Write "Gutentag"      Case Else           Response.Write "Sorry We'd            love to say hello, but we don't            know how" End Select As you can see from above, there are four parts to the Select Case statement. State which variable should be tested, in this case strLanguage State a possible value and what to do when the variable evaluates to true. The possible value comes after "Case" Repeat as necessary End the Select Case structure using "End Select"

4 Select case Basic Select Case Rules:
Use Select Case when there are more than two possibilities The first line states the variable to be evaluated Case lines give a value and a comparitor to check against. ASP will execute and exit after the first Case match. Example

5 For .. next For...Next Loop You can use a For...Next statement to run a block of code, when you know how many repetitions you want. You can use a counter variable that increases or decreases with each repetition of the loop, like this: For i=1 to 10 some code Next The For statement specifies the counter variable (i) and its start and end values. The Next statement increases the counter variable (i) by one.

6 For.. next Step Keyword Using the Step keyword, you can increase or decrease the counter variable by the value you specify. In the example below, the counter variable (i) is increased by two each time the loop repeats. For i=2 To 10 Step 2 some code Next To decrease the counter variable, you must use a negative Step value. You must specify an end value that is less than the start value. In the example below, the counter variable (i) is decreased by two each time the loop repeats. For i=10 To 2 Step -2

7 For each… Next A For Each...Next loop repeats a block of code for each item in a collection, or for each element of an array. dim cars(2) cars(0)="Volvo" cars(1)="Saab" cars(2)="BMW" For Each x in cars Response.write(x & "<br />") Next

8 While Wend While...Wend Statement
Executes a series of statements as long as a given condition is True. While condition    Version [statements] Wend The following example illustrates use of the While...Wend statement: Dim Counter Counter = 0   ' Initialize variable. While Counter < 20   ' Test value of Counter.     Counter = Counter + 1   ' Increment Counter.   Alert Counter Wend   ' End While loop when Counter > 19.

9 Do While Do...Loop You can use Do...Loop statements to run a block of code when you do not know how many repetitions you want. The block of code is repeated while a condition is true or until a condition becomes true. Repeating Code While a Condition is True You use the While keyword to check a condition in a Do...Loop statement. Do While i>10 some code Loop If i equals 9, the code inside the loop above will never be executed.

10 Do.. until You use the Until keyword to check a condition in a Do...Loop statement. Do Until i=10 some code Loop If i equals 10, the code inside the loop will never be executed. Do Loop Until i=10 The code inside this loop will be executed at least one time, even if i is equal to 10.

11 Procedures One of the techniques used for effective programming is to divide a big assignment in (relatively small) sub-assignments. Each sub-assignment is meant to (possibly completely) solve a particular problem so that other sub-assignments of the program can simply request its result or refer to it when necessary. Such a sub-assignment is called a procedure. example

12 Functions Like a sub procedure, a function is used to perform an assignment. The main difference between a sub procedure and a function is that, after carrying its assignment, a function gives back a result. We also say that a function "returns a value". To distinguish both, there is a different syntax you use for a function.


Download ppt "ASP control structure BRANCHING STATEMENTS"

Similar presentations


Ads by Google