Latest web development tutorials

ASP.NET Razor VB logic

Logic Programming: execute code conditionally.


If conditions

VB permitted under the condition code execution.

Use an if statement to determine the conditions. According to the judgment result, if statement returns true or false:

  • if statement starts a code block
  • Conditions written between if and then
  • The code if the condition is true, if ... then and is performed between the end if

Examples

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

Running instance »


Else Condition

else if statement may contain conditions.

else conditions defined code if the condition is false to be executed.

Examples

@Code
Dim price=20
End Code
<html>
<body>
@if price>30 then
@<p>The price is too high.</p>
Else
@<p>The price is OK.</p>
End If
</body>
</htmlV>

Running instance »

Note: In the example above, if the first condition is true, if the code block will be executed. else if conditions are covered in addition to the condition of "all other cases."


ElseIf conditions

You can use multiple criteria to judge elseif conditions:

Examples

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

Running instance »

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

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

Unlimited number of elseif conditions.

If the if and elseif conditions are not true, the last else block (without conditions) covers "all other cases."


Select Condition

select blocks can be used to test a number of separate conditions:

Examples

@Code
Dim weekday=DateTime.Now.DayOfWeek
Dim day=weekday.ToString()
Dim message=""
End Code
<html>
<body>
@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>
</body>
</html>

Running instance »

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

select block has a default (Case Else), when all the specified circumstances do not match, it covers "all other cases."