ASP Basics Tutorial: ASP script variables, functions, procedures, and conditional statements

Source: Internet
Author: User
Tags case statement square root

In the previous issue, the author gave you a brief introduction to the ASP scripting language of one of VBScript's basic knowledge, this issue will continue to explain the scripting method of VBScript, and by showing VBScript in the ASP program in the process of writing a series of examples to make people to VBScript have more into a layer of understanding.
After learning the basic concepts of variables, constants, and procedures for scripting language VBScript, we will continue to introduce you to VBScript's functions and syntax in this issue.
Functions and procedures are named blocks of code, but they are very different, the procedure completes the program task, and the function returns the value. We can understand that the process is like a complete sentence, and the function is like a word. For example, when you want to get the square root of a number, you simply pass that number to VBScript's SQR () function, which returns the square root of the number immediately. Such as:
A=SQR (9)    The a=3. Proficiency in scripting language functions will give you the ability to write ASP programs to bring great convenience, on the end of the above phase of the author assigned to everyone's after-school exercises, if you do not complete the script language functions, then solve such a small problem will likely cost you a considerable amount of energy. Now let's review this lesson after practice.
"The author is using ASP to make a WEB-based BBS system, hoping to add a special function, that is, when any user login to the BBS will be able to access all the newly released information for nearly seven days." ”
If you are unfamiliar with Vbscrip, you will not know that the Vbscrip itself provides a function DateSerial to get the difference between dates, and its syntax is as follows:
DateSerial (year, month, day)    If you want to specify a date, for example: November 10, 1998, then the range of values for each parameter in the DateSerial function should be acceptable, and the value for the same day should be between 1 and 31, and the month value should be between 1 and 12. However, you can also specify a relative date for each parameter by using a numeric expression that represents the number of years, months, and days before or after a day. The following example uses a numeric expression instead of an absolute date. Here, the DateSerial function returns the date of 20 years (1990-20) 2 months (11-2) and another day (10-1) before November 10, 1998: September 9, 1978. The procedure is as follows:
Datep=dateserial (1998-20, 11-2,10-1)    For the year parameter, if the value range is from 0 to 99, it is interpreted as 1900-1999. For the year parameter outside of this range, a four-bit number is used to represent the years (for example, 1800). When the value of any one parameter exceeds an acceptable range, it is properly rounded to the next larger unit of time. For example, if you specify 35 days, the number of days will be interpreted as a month plus a number of days, and the number of days to come out depends on the year and month. However, an error occurs if the value of the parameter exceeds the range of 32,768 to 32,767, or if the date specified by three parameters, whether directly or by an expression, exceeds the acceptable date range.
When we understand and grasp the function of the use of DateSerial, and then to see the author's layout of the problem, everything will be solved. Below I will publish this part of the code in the program as follows:Itemp=dateserial (date), month (date), day (date)-7)
Itemp=datevalue (ITEMP)Sql= "SELECT * from Message Where message.creatime between #" &date& "# and #" &itemp& "#"    Here we are exposed to a set of function Year,month,day, which are used to get a date for the year, month, and day. Date is a constant that represents today's date, whereas the function DateValue is a variable that converts a string variable into a date format. In the third line of this paragraph, we first contacted the standard SQL query statement, what does this sentence mean?
"Select" is the standard SQL database query command, through the SELECT statement we can retrieve data in the database and provide the query results to the user, where "*" means querying all records in the database named "Message", and "where" is to set a query condition to take out the eligible records in the database, and "Message.creatime" is a variable that stores the creation date of the record in the database. To connect an entire sentence to understand is to query all records in a database named message, and store all records in the variable SQL with the creation date within 7th of today and today. Probably because the first time you touch the SQL statement, the time can not fully understand its role, but do not worry in future chapters will be dedicated to the author of the use of SQL to introduce you.
Through the above study, we should have been able to understand the function in the program, of course, we do not have to die back function, but to be proficient in using only a shortcut-more practice. Now let's look at the basic syntax of VBScript.
A friend who knows the programming language must know that the statements in the program process can be divided into conditional statements and loop statements, and the following conditional statements can be used in VBScript: If ... Then ... Else statement
Select Case Statement
If ... Then ... The Else statement is used to evaluate whether the condition is True or False and to specify the statement to run based on the result of the calculation. Typically, a condition is an expression that compares a value or variable with a comparison operator, If ... Then ... Else statements can be nested as needed.
Let's create two sample files: If1.asp and if2.asp
Paste the following statement into your Notepad and save it as if1.asp (note: Remove the space after "<" in your program) < html>
< head>< title>if1.asp</title>< form action= "if2.asp" method=get>Your first name< INPUT name= "FirstName" maxlength=20>< p>Your last name< INPUT name= "LastName" maxlength=20>< p>< input type=submit>< input type=reset></form></body>
Paste the following statement into your Notepad and save it as if2.asp
< Html> < Head> < title>ifrespond.asp</title> <% fname=request.querystring ("Firstname") lname=request.querystring (" Lastname ") If fname=" George "and Lname=" Washington "then%> Hi.you must is the first President! <% else%> hi! Nice to Meet you <%end if%> </body >

asp1.asp produces a text input box requiring the user to enter a last name, first name, such as:

Asp2.asp is to use the IF statement to determine whether the user entered the name is "George Washington", and make corresponding feedback. Here we come across an ASP's built-in object request that can be accessed by using the request object, including parameters, cookies, and user authentication passed from the HTML table using the POST method or the GET method. The QueryString collection retrieves the values of the variables in the HTTP query string, and the HTTP query string is specified by the value after the question mark (?). Such as:
Http://localhost/if2.asp?Firstname=George&Lastname=Washington
Generates a variable name string with a value of "Firstname=george&lastname=washington". The author of the ASP object will focus on the next few days.
If ... Then ... Another variant of the Else statement allows you to choose from multiple criteria, which is to add the ELSEIF clause to extend the If ... Then ... Else statement, which allows you to control the flow of programs based on a variety of possible processes.
We will expand the program portion of the asp2.asp as follows:<%
Fname=lcase (Request.QueryString ("Firstname"))Lname=lcase (Request.QueryString ("Lastname"))If fname= "George" and Lname= "Washington" then%>Hi.you must be the first president!< p><% elseIf Fname= "Ronald" and Lname= "Reagan" then%>Hi.you must be the actor president!< p><% elseIf Fname= "Jimmy" and Lname= "Carter" then%>Hi.you must be the peanut farmer president!< p><% elseIf Fname= "Naoko" or Fname= "Charles" then%>Hi.your name reminds me of Someone,but I am not sure who!< p><% Else%>Hi! Nice to Meet<% End If%>
You can add any number of ElseIf clauses to provide multiple choices. However, using multiple ElseIf clauses often makes the program cumbersome. A better way to make a selection in multiple conditions is to use the Select Case statement.
The Select case structure provides the If ... Then ... A workaround for the ELSEIF structure, which can be selected from multiple statement blocks to perform one of them. The Select Case statement provides functionality with the If ... Then ... Else statements are similar, but can make the code more concise and easy to read. The Select case structure uses a simple test expression that evaluates only once at the beginning of the case. The result of an expression is compared to the value of each case in the structure. If a match is performed, the statement block associated with the case is executed, and we can also write the asp2.asp file with the SELECT clause statement:
<%Fname=lcase (Request.QueryString ("Firstname"))Lname=lcase (Request.QueryString ("Lastname"))Name=fname+lnameSelect Case NameCase "Georgewashington"Response.Write "Hi.you must be the first president!< p>"Case "Ronaldreagan"Response.Write "Hi.you must be the actor president!< p>"Case "Jimmycarter"Response.Write "Hi.you must be the peanut farmer president!< p>"Case "Naokocharles"Response.Write "Hi.your name reminds me of Someone,but I am not sure who!< p>"Case Else Response.Write "hi! Nice to Meet you " End Select%> note that the Select case structure computes only one expression at the beginning, and evaluates only once, and If ... Then ... The ELSEIF structure calculates the expressions for each ELSEIF statement, which can vary. Therefore, you can use the Select case structure instead of the If ... only if the expression evaluated by each ELSEIF statement is the same. Then ... ELSEIF structure. The Select Case statement can also be nested, and each layer of nested Select Case statements must have an END SELECT statement that matches it.
above to introduce you to the scripting language VBScript's function and conditional statement use method, because of the length of the reason can not be detailed, I hope you have to learn ASP friends, Can do a certain degree of self-study and practice after class. In the process of developing ASP application, the author gradually realizes the importance of scripting language, and the flexible use of scripting language will greatly improve the development process of ASP application, save a lot of time for the production staff of the website, and also can enhance the efficiency and function of ASP application execution. To its prerequisite, so the author strongly recommends gentlemen, proficiency in scripting language, which will help you asp program development greatly. Since this article is not a VBScript tutorial, so we can only use a small space to give you a brief introduction to some of VBScript basic knowledge, in the next issue of VBScript after the Loop statement, we will formally start learning ASP built objects, to deep VBScript, We recommend that you find some textbooks for self-study. If you have any questions after reading this article please mail me in time, if you have any good suggestions also please write to inform, thank you.

ASP Basic Tutorial: ASP script variables, functions, procedures, and conditional statements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.