Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

asp
.net
communitycreator

What is ASP looping?

Praise Balogun

“Imagine exploring one of the wonders of software engineering by repeatedly performing a single task as if you did it just once.”

One of the most interesting things about software engineering lies in the ability to automate a repeated task. So, let’s think about automatic repetitions and looping.

What is a loop?

A loop is a common term used for repeated activities. Every programming language has methods to automate repeated activities. It is important to note here that every loop statement must be used with a constraint. This constraint is always a condition that must be true for that loop to terminate. In other words, the loop will continue as long as the condition is false.

ASP looping statements

ASP .Net is a common framework used to build web applications. In ASP, there are several looping statements. Here are a few:

  • For…Next statement. This type of loop runs a block of code a specified number of times. To achieve this kind of loop, an initial counter variable is set. Then, another value is set that represents the constraint. For each iteration, the counter variable is increased until its value equals the constraint. At this point, the loop will stop.
<html>
<body>
 <%
    For i=1 To 5
    response.write("hello ") 
    Next
 %>
</body>
</html>

In the code block above, the initial counter value is set to 1. This means that the count starts from 1 and then prints the word ‘hello’ until the counter value gets to 5.

The step keyword

The step keyword can be used with the For…Next statement. With the step keyword, you can increase or reduce the counter variable by a specified value.

For i=10 To 2 Step -2
  response.write("hello ") 
Next
  • For Each…Next statement. This runs a block of code for the individual items in a collection. A good example of a list could is an array of numbers.

  • Do…Loop statement. In this type of loop statement, the block of code loops until a given condition turns true. This loop depends solely on a true or false condition.

Exit keyword

The Exit keyword is an extra condition that is used to break out of a loop. For example, the code below will run as long as it is not equal to or greater than 5.

Do Until i=5
  i=i-1
  If i<5 Then Exit Do
Loop
<html>
<body>
<%
For i = 0 To 10
  response.write("The number is " & i & "<br />")
Next
%>
</body>
</html>
A simple example of a For...Next statement
<html>
<body>

<%
Dim animals(2)
animals(0)="Dog"
animals(1)="Cat"
animals(2)="Monkey"

For Each animal In animals
  response.write(animal & "<br />")
Next
%>

</body>
</html>
Example of a For Each...Next loop

RELATED TAGS

asp
.net
communitycreator
RELATED COURSES

View all Courses

Keep Exploring