Chapter 32: ASP Conditional
1. What are Conditional Statements in VBScript?
Conditional statements let your code make decisions — they ask questions and run different blocks of code depending on the answer.
In VBScript (the default language inside Classic ASP .asp files), the two main conditional tools are:
| Statement | When to use it | Most common in real Classic ASP code? |
|---|---|---|
| If … Then … Else … End If | 1 condition, or a small chain of conditions | Yes — by far the #1 choice |
| Select Case | Many possible fixed values (like a switch) | Yes, but used less often |
There is no ternary operator (?:) in VBScript — people use the inline If(condition, trueValue, falseValue) function instead.
2. The Most Important One: If … Then … Else … End If
This is the workhorse of Classic ASP — you will see it on almost every serious .asp page.
Basic syntax patterns
Single-line If (short & simple)
|
0 1 2 3 4 5 6 |
<% If hour >= 18 Then Response.Write "Good Evening!" %> |
Multi-line If (most common in real code)
|
0 1 2 3 4 5 6 |
<% Dim hour hour = Hour(Now()) If hour < 12 Then Response.Write "<p style='color:orange;'>Good Morning! ☀️</p>" ElseIf hour < 17 Then Response.Write "<p style='color:green;'>Good Afternoon! 🌤️</p>" Else Response.Write "<p style='color:blue;'>Good Evening! 🌙</p>" End If %> |
With HTML mixed in (very typical Classic ASP style)
|
0 1 2 3 4 5 6 |
<h2>Welcome to our site</h2> <% Dim isLoggedIn isLoggedIn = (Session("UserID") <> "") If isLoggedIn Then %> <p>Hello, <%= Server.HTMLEncode(Session("UserName")) %>! <a href="logout.asp">Logout</a></p> <% Else %> <p>Please <a href="login.asp">login</a> or <a href="register.asp">register</a>.</p> <% End If %> |
→ Notice how the HTML is outside the <% %> blocks — this is classic style.
3. Real-World Example 1 – Show Discount Message
|
0 1 2 3 4 5 6 |
<% Dim totalAmount, discountRate, finalAmount totalAmount = 1499.00 discountRate = 0 If totalAmount >= 2000 Then discountRate = 15 ElseIf totalAmount >= 1000 Then discountRate = 10 ElseIf totalAmount >= 500 Then discountRate = 5 End If finalAmount = totalAmount * (1 - discountRate / 100) Response.Write "<p>Order total: ₹" & FormatNumber(totalAmount, 2) & "</p>" If discountRate > 0 Then Response.Write "<p style='color:green; font-weight:bold;'>" Response.Write "Congratulations! You get " & discountRate & "% discount!" Response.Write "</p>" Response.Write "<p>Final amount after discount: ₹" & FormatNumber(finalAmount, 2) & "</p>" Else Response.Write "<p style='color:gray;'>Add more items to unlock discount!</p>" End If %> |
4. The Other Tool: Select Case (VB’s switch)
Used when you have many fixed possible values.
|
0 1 2 3 4 5 6 |
<% Dim dayNumber dayNumber = Weekday(Now()) ' 1=Sunday, 2=Monday, ..., 7=Saturday Select Case dayNumber Case 1 Response.Write "<p>Today is Sunday – Weekend mode ON! 🏖️</p>" Case 2 Response.Write "<p>Monday – Let's start the week strong! 💪</p>" Case 3 To 5 Response.Write "<p>Working day – Keep going!</p>" Case 6, 7 Response.Write "<p>Weekend again! Enjoy Hyderabad! 🌟</p>" Case Else Response.Write "<p>Unknown day – something is wrong!</p>" End Select %> |
Select Case is cleaner than long ElseIf chains when checking the same variable against many values.
5. Inline Conditional (the VB “ternary”)
VBScript has no ?: operator — instead use the IIf function or the normal If as expression.
|
0 1 2 3 4 5 6 |
<p>Status: <%= IIf(Session("LoggedIn") = True, "Online", "Offline") %></p> <!-- or more safely --> <p>Status: <%= If(Session("LoggedIn") = True, "Online", "Offline") %></p> |
IIf is older, but still very common in Classic ASP code.
6. Common Patterns You Will See in Real Classic ASP Sites
- Check login status
|
0 1 2 3 4 5 6 |
<% If Session("UserID") = "" Then Response.Redirect "login.asp" %> |
- Show different navigation
|
0 1 2 3 4 5 6 |
<% If isAdmin Then %> <a href="admin.asp">Admin Area</a> <% End If %> |
- Highlight active menu item
|
0 1 2 3 4 5 6 |
<a href="products.asp" <%= If currentPage = "products" Then "class='active'" %> >Products</a> |
- Error / success message after form submit
|
0 1 2 3 4 5 6 |
<% If Request.Form("submitted") = "yes" Then %> <% If success Then %> <div class="success">Thank you!</div> <% Else %> <div class="error">Something went wrong.</div> <% End If %> <% End If %> |
7. Teacher Summary – VBScript Conditional Statements in Classic ASP
VBScript Conditional Statements (used inside Classic ASP):
- If … Then … ElseIf … Else … End If → most common, supports blocks of HTML
- Select Case … Case … Case Else … End Select → cleaner for many fixed values
- Inline: If(…, …, …) or older IIf(…, …, …)
- Always use Option Explicit to avoid bugs
- Use Server.HTMLEncode when outputting strings
- Mix HTML outside <% %> blocks — very typical Classic ASP style
This is how millions of pages decided what to show users in the 2000s — and many old Indian business/internal systems still use exactly this logic in 2026.
Questions for next?
- Want a full example with login check + role-based menu using If?
- Or compare Classic ASP If vs Razor @if side-by-side?
- Or move to the next W3Schools topic (ASP Loops)?
- Or see how errors were handled with conditions in old code?
Just tell me — I’m here! 🚀🇮🇳 Keep going strong, Webliance! 😊
