Easy Tutorial
❮ Control Calendarday Prop Webcontrol Button Validationgroup ❯

ASP.NET Razor - VB Logical Conditions


Programming Logic: Execute code based on conditions.


If Condition

VB allows code execution based on conditions.

Use the if statement to evaluate conditions. Based on the evaluation result, the if statement returns true or false:

Example

```@Code Dim price = 50 End Code <html> @If price > 30 Then @<p>The price is too high.</p> End If </body>


---

## Else Condition

The if statement can include an **else condition**.

The else condition defines the code to be executed when the condition is false.

## Example
```@Code
Dim price = 20
End Code
<html>
@If price > 30 Then
    @<p>The price is too high.</p>
Else
    @<p>The price is OK.</p>
End If
</body>

Note: In the above example, if the first condition is true, the code in the if block will be executed. The else condition covers "all other cases" except the if condition.


ElseIf Condition

Multiple conditions can be evaluated using the elseif condition:

Example

```@Code Dim price = 25 End Code <html> @If price >= 30 Then @<p>The price is high.</p> ElseIf price > 20 And price < 30 Then @<p>The price is OK.</p> Else @<p>The price is low.</p> End If </body>


In the above example, if the first condition is true, the code in the if block will be executed.

If the first condition is not true and the second condition is true, the code in the elseif block will be executed.

There is no limit to the number of elseif conditions.

If neither the if nor the elseif conditions are true, the final else block (without a condition) covers "all other cases."

---

## Select Condition

The **select block** can be used to test individual conditions:

## Example
```@Code
Dim weekday = DateTime.Now.DayOfWeek
Dim day = weekday.ToString()
Dim message = ""
End Code
<html>
@Select Case day
    Case "Monday"
        message = "This is the first weekday."
    Case "Thursday"
        message = "Only one day before weekend."
    Case "Friday"
        message = "Tomorrow is weekend!"
    Case Else
        message = "Today is " & day
End Select
<p>@message</p>

"Select Case" is followed by the test value (day). Each individual test condition has a case value and any number of code lines. If the test value matches the case value, the corresponding code lines are executed.

The select block has a default case (Case Else) that covers "all other cases" when none of the specified cases match.

❮ Control Calendarday Prop Webcontrol Button Validationgroup ❯