Razor Tutoriallogin
Razor Tutorial
author:php.cn  update time:2022-04-11 14:21:21

Razor VB loop


ASP.NET Razor - VB Loops and Arrays


statements will be executed repeatedly in the loop.


For Loop

If you need to execute the same statement repeatedly, you can set a loop.

If you know the number of times you want to loop, you can use for loop. This type of loop is especially useful when counting up or down:

Instance

<html>
<body>
@for i=10 to 21
   @<p>Line @i</p>
next i
</body>
</html>

Running Example»

Click the "Run Example" button to view the online example


For Each Loop

If you are using a collection or array, you will often usefor each cycle.

A collection is a group of similar objects, and the for each loop can traverse the collection until completion.

In the following example, the ASP.NET Request.ServerVariables collection is traversed.

Instance

<html>
<body>
<ul>
@for each x in Request.ServerVariables
    @<li>@x</li>
next x
</ul>
</body>
</html>

Run Instance»

Click the "Run Instance" button to view the online instance


While loop

while loop is a general loop.

The while loop starts with the while keyword, followed by parentheses where you specify how long the loop will last, and then the block of code to be executed repeatedly.

The while loop usually sets an incrementing or decrementing variable for counting.

In the following example, the += operator adds 1 to the value of variable i each time the loop is executed.

Instance

<html>
<body>
@Code
Dim i=0
Do While i < 5
    i += 1
    @<p>Line @i</p>
Loop
End Code
</body>
</html>

Run Instance»

Click the "Run Instance" button to view the online instance



Array

When you want to store multiple similar variables but don’t want to create a separate variable for each variable, you can use an array to store:

Instance

@Code
Dim members as String()={"Jani","Hege","Kai","Jim"}
i=Array.IndexOf(members,"Kai")+1
len=members.Length
x=members(2-1)
End Code
<html>
<body>
<h3>Members</h3>
@For Each person In members
    @<p>@person</p>
Next person
<p>The number of names in Members are @len</p>
<p>The person at position 2 is @x</p>
<p>Kai is now in position @i</p>

</body>
</html>

Run instance»

Click the "Run instance" button to view the online instance


php.cn