Asp.net Classic

Conditional

Asp.net Classic / Conditional

Conditional

Conditional Statements in VBScript

 

Conditional statements are used to make decisions in a program. Based on whether a condition is true or false, different blocks of code are executed.

VBScript provides the following four conditional statements:

If statement – Executes code when a condition is true

If…Then…Else statement – Chooses between two blocks of code

If…Then…ElseIf statement – Selects one block from multiple conditions

Select Case statement – Executes one block from several possible options

If…Then…Else Statement

 

Use the If…Then…Else statement when you want to run code only when a condition is satisfied or choose between two alternatives.

Single-Line If Statement

If only one statement needs to be executed when the condition is true, it can be written on a single line:

 

If i = 10 Then Response.Write("Hello")

In this case, the statement executes only when i equals 10. There is no Else part here.

Multi-Line If Statement

 

When more than one statement needs to be executed, the code must be written on separate lines and closed with End If:

 

If i = 10 Then   Response.Write("Hello")   i = i + 1 End If

This code runs all the statements inside the block if the condition is true.

If…Then…Else Example

 

To execute one block when the condition is true and another block when it is false, use Else:

 

i = Hour(Time) If i < 10 Then   Response.Write("Good morning!") Else   Response.Write("Have a nice day!") End If

Here, the first message is displayed if the time is before 10, otherwise the second message is shown.

If…Then…ElseIf Statement

 

When multiple conditions need to be checked, the ElseIf keyword can be used:

 

i = Hour(Time) If i = 10 Then   Response.Write("Just started...!") ElseIf i = 11 Then   Response.Write("Hungry!") ElseIf i = 12 Then   Response.Write("Ah, lunch-time!") ElseIf i = 16 Then   Response.Write("Time to go home!") Else   Response.Write("Unknown") End If

Each condition is evaluated in order, and the block associated with the first true condition is executed.

Select Case Statement

 

The Select Case statement is another way to choose one block of code from multiple options. It is often clearer than using many ElseIf statements.

Example:

 

d = Weekday(Date) Select Case d   Case 1      Response.Write("Sleepy Sunday")   Case 2      Response.Write("Monday again!")   Case 3      Response.Write("Just Tuesday!")   Case 4      Response.Write("Wednesday!")   Case 5      Response.Write("Thursday...")   Case 6      Response.Write("Finally Friday!")   Case Else      Response.Write("Super Saturday!!!!") End Select

How Select Case Works

 

A single expression (usually a variable) is evaluated once

Its value is compared against each Case

When a match is found, the corresponding block of code is executed

If no case matches, the Case Else block runs

Technology
Asp.net Classic
want to connect with us ?
Contact Us