Free Webmaster Help Logo
Tutorials Articles Tools Images Directory Ask Newsletter
*
 

Tutorials
Cookies
ASP
Advanced HTML
Frames & Tables
.htaccess
Rounded Table Corners
Flash 5
FTP
Beginners
Javascript
PHP/MySQL
Site Promotion
Mobile Internet: WML/WAP
Server Side Includes (SSI)
HTML - The Basics
XHTML
Stylesheets
XML
PHP
*

125x125

Related Links
More ASP Sites
*

 
Sitemap | Contact | Link To Us | Advertise
Report A Problem
Home : Tutorials : ASP : Part 3

ASP Tutorial
Part 3 - IF Statements

Introduction

Over the past two parts I have shown you the basics of text in ASP and how to store it as variables. In this part of the tutorial I will show you how to use IF statements to make decisions in your scripts.

The Basics Of IF

If statements are used to compare two values and carry out different actions based on the results of the test. If statements take the form IF, THEN, ELSE. Basically the IF part checks for a condition. If it is true, the then statement is executed. If not, the else statement is executed.

IF Strucure

The structure of an IF statement is as follows:

If something=somethingelse Then
Execute some code
Else
Execute other code
End If


Common Comparisions

The ASP IF statement construction is very much like plain text, but here is a quick example of a common use of ASP. In this example the user has entered a password which has been stored in the variable EnteredPassword. The idea of this script it to check whether the user has entered the correct password:

<%@ Language=VBScript %>
<%
If EnteredPassword="password1" Then
Response.Write("Well done. You got the password right.")
Else
Response.Write("Sorry. That was the wrong password.")
End If
%>

If the user enters the correct password (password1) the text:

Well done. You got password right.

but if you get it incorrect you will be shown the text:

Sorry. That was the wrong password.


Other IF Options

There are many of different comparisions you can make with ASP, for example you can comapre two variables:

If EnteredPassword=RealPassword Then

or different types of comparison:

If Age>13 Then

which will check to see if the age entered by the user is greater than 13.

You can also place HTML etc. in IF statements, as the ASP will continue executing a THEN statement until it reaches an Else or an End If, and will continue to execute Else statements until it reaches End If, for example:

<%
If EnteredPassword="password1" Then
%>
<font face="Arial" size="3">Congratulations. You may enter.</font>
<%
Else
%>
<font face="Arial" size="5" color="Red">ERROR! You cannot enter.</font>
<%
End If
%>


Part 4

Now you can output text, place text and numbers in variables and execute conditional statements. In the next part I will show you how to operate loops to repeat sections of code.

© 1999 - 2001 FreeWebmasterHelp.com