X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

ASP.NET Razor - VB Logic Conditions


Programming Logic: Execute code based on conditions.


The If Condition

VB lets you execute code based on conditions.

To test a condition you use the if statement. The if statement returns true or false, based on your test:

  • The if statement starts a code block
  • The condition is written between if and then
  • The code between if ... then and end if is executed if the test is true

Example

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

Run example »


The Else Condition

An if statement can include an else condition.

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

Example

@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>

Run example »

Note: In the example above, if the first condition is true, it will be executed. The else condition covers "everything else".


The ElseIf Condition

Multiple conditions can be tested with an else if condition:

Example

@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>

Run example »

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

If not, then if the next condition is true, this condition will be executed.

You can have any number of else if conditions.

If none of the if or else if conditions are true, the last else block (without a condition) covers "everything else".


Select Conditions

A select block can be used to test a number of individual conditions:

Example

@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>

Run example »

"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 code lines are executed.

A select block can have a default case (Case Else) for "everything else" that runs if none of the other cases are true.



Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates FREE Website Templates Free HTML Templates

SHARE THIS PAGE

facebook