WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

ASP.NET Razor - C# Loops and Arrays


Statements can be executed more than once in loops.


For Loops

If you have to run similar proclamations more than once, you can program a loop.

If you know how frequently you need to circle, you can utilize a for loop. This sort of circle is particularly valuable for checking up or tallying down:

Example

<html>
<body>
@for(var I = 10; I < 21; i++)
    {<p>Line @i</p>}
</body>
</html>

For Each Loops

If you work with an accumulation or a cluster, you regularly utilize a for each loop.

A gathering is a gathering of comparable articles, and the for each circle lets you do an undertaking on every thing. The for each circle strolls through a accumulation until it is finished.

The model beneath strolls through the ASP.NET Request.ServerVariables collection.

Example

<html>
<body>
<ul>
@foreach (var x in Request.ServerVariables)
    {<li>@x</li>}
</ul>
</body>
</html>

While Loops

The while loop is a broadly useful circle.

A while circle starts with the while watchword, trailed by brackets, where you indicate to what extent the circle proceeds, at that point a square to repeat.

While circles normally add to, or subtract from, a variable utilized for counting.

In the precedent beneath, the += administrator adds 1 to the variable I, each time the circle runs.

Example

<html>
<body>
@{
var I = 0;
while (I < 5)
    {
    I += 1;
    <p>Line @i</p>
    }
}

</body>
</html>

Arrays

An exhibit is helpful when you need to store comparable factors yet would prefer not to make a different variable for each of them:

Example

@{
string[] individuals = {"Jani", "Hege", "Kai", "Jim"};
int I = Array.IndexOf(members, "Kai")+1;
int len = members.Length;
string x = members[2-1];
}
<html>
<body>
<h3>Members</h3>
@foreach (var individual in members)
{
<p>@person</p>
}

<p>The number of names in Members are @len</p>
<p>The individual at position 2 is @x</p>
<p>Kai is currently in position @i</p>
</body>
</html>