“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.
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 .Net is a common framework used to build web applications. In ASP, there are several looping statements. Here are a few:
<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 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.
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 10response.write("The number is " & i & "<br />")Next%></body></html>
<html><body><%Dim animals(2)animals(0)="Dog"animals(1)="Cat"animals(2)="Monkey"For Each animal In animalsresponse.write(animal & "<br />")Next%></body></html>