SQL PHP HTML ASP JavaScript articles and free scripts to download
 

If Else and nested If conditional script control


To control the structure and flow of script we use if then else conditional statements of VBScript used in ASP. By using different combinations of if else condition we can control the program execution flow as per requirement. Before going into more details on this let us try the basic syntax of if – condition – Then – Else – Endif syntax.

If condition Then
Script block 1 here if condition is true
Else
Script block 2 here if condition is false
EndIf


In the above script block we have first tested the condition and if it is TRUE then Script block 1 gets executed and if the condition is FALSE then script block 2 is executed.

Here is a simple sample code for printing greatest of two numbers.

Dim n1,n2
n1=25
n2=45
If n1 > n2 Then
Response.Write " Greatest number is " & n1
Else
Response.Write " Greatest number is " & n2
End If


By changing the assigned values to n1 and n2 we can check how the if else control structure works.

We can keep If conditions inside another If conditions but each If condition has to have matching End If condition. We call it nested If conditions. The above code is changed to add one more If condition inside main If condition.

Dim n1,n2
n1=-25
n2=-45
If n1 > n2 Then
Response.Write " Greatest number is " & n1
If n1 < 0 Then
Response.Write " First number is a negetive number "
End IF

Else
Response.Write " Greatest number is " & n2
End If


Some places we can use ElseIF condition than using more nested If Else condition. Here is an example of uses of ElseIf Condition.

Dim my_num
my_num=40
If my_num < 100 Then
Response.Write " It is less than 100" & "<br>"
If my_num < 50 Then
Response.Write " It is less than 50 also" & "<br>"
End If


ElseIf my_num > 200 Then
Response.Write " It is greater than 200 " & "<br>"
Else
Response.Write " This is greater than 100 but less than 200" & "<br>"
End if


Note that ElseIf doest not require an End If to close but the original If condition should have its own.

ElseIf condition can't be used just after one Else condition. Where as Else condition can be used before ElseIF condition.


Further readings
File Included using relative and virtual in ASP
If Else & ElseIF conditional program structure control in ASP
Matching options by using Select Case statement in ASP
Do While Wend Loops in ASP
For Next Loop in ASP
Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
All ASP Tutorials
ASP Sections
Ajax
Date and time
Declaring array
Form in ASP
Server.MapPath
Date Time & MSSQL
Select Query
File System Object
String Functions
Popular Tutorials
Managing two drop downs
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.