WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

ASP.NET Razor - VB Logic Conditions


Programming Logic: Execute code dependent on conditions.


The If Condition

VB gives you a chance to execute code dependent on conditions.

To test a condition you utilize the if statement. The if proclamation returns genuine or false, in view of your test:

  • The if proclamation begins a code block
  • The condition is composed among if and then
  • The code between if ... at that point 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 cost is too high.</p>
End If
</body>
</html>
Run model »

The Else Condition

An if explanation can incorporate a else condition.

The else condition characterizes 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 cost is too high.</p>
Else
    @<p>The cost is OK.</p>
End If
</body>
</html>

Note: In the precedent above, if the primary condition is valid, it will be executed. The else condition covers "everything else".


The ElseIf Condition

Multiple conditions can be tried with a else if condition:

Example

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

In the precedent above, if the principal condition is valid, it will be executed.

If not, at that point if the following condition is valid, this condition will be executed.

You can have any number of else if conditions.

If none of the if or disaster will be imminent if conditions are valid, the last else square (without a condition) covers "everything else".


Select Conditions

A select block can be utilized to test various 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 precedent »

"Select Case" is trailed by the test esteem (day). Every individual test condition has a case esteem, and any number of code lines. On the off chance that the test esteem coordinates the case esteem, the code lines are executed.

A select square can have a default (Case Else) for "everything else" that runs if none of alternate cases are valid.