ASP.NET Basics, Part 4: Looping the Loop - The Sound of Breaking Loops
(Page 6 of 7 )
When dealing with loops, there are two important keywords you should beaware of: "break" and "continue". You might remember that I dealt withthese briefly in the previous section of this tutorial; let's now take acloser look, since these keywords come in handy when dealing with loopsas well.
First, the "break" keyword. This is usually used to exit a loop when itencounters an unexpected situation. A good example of such a situationis the dreaded "division by zero" error: when dividing one number byanother one (which keeps decreasing), it is always advisable to checkthe divisor on every iteration and take action (either exit the loop orskip to the next iteration) once it becomes equal to zero.
Take a look:
<script language="C#" runat="server">
void Page_Load()
{
// some variables to play with
int dividend = 7;
int divisor = 10;
// a for loop to perform division
for(int count = 1; count <= 15; count++)
{
// if divisor is zero
// get out now
if(divisor == 0)
{
break;
}
// print loop counter and perform division
output.Text += "Loop counter is " + count + "." + " ";
output.Text += dividend + " / " + divisor + " = " +
((double)dividend/divisor) + "
";
// decrement divisor
divisor--;
}
output.Text += "<h3>Divisor is zero, time to stop!</h3>";
}
</script>
<html>
<head></head>
<body>
<h3>Divide And Conquer</h3>
<asp:label id="output" runat="server" />
</body>
</html>
Here's the output:
Divide And Conquer
Loop counter is 1. 7/10 = 0.7
Loop counter is 2. 7/9 = 0.777777777777778
Loop counter is 3. 7/8 = 0.875
Loop counter is 4. 7/7 = 1
Loop counter is 5. 7/6 = 1.166666666666667
Loop counter is 6. 7/5 = 1.4
Loop counter is 7. 7/4 = 1.75
Loop counter is 8. 7/3 = 2.333333333333333
Loop counter is 9. 7/2 = 3.5
Loop counter is 10. 7/1 = 7
Divisor is zero, time to stop!
In this example, I have defined two variables, a dividend and a divisor.Next, I've used a "for" loop to divide the two values by each other afixed number of times (15). Note that I have also decremented the valueof the "divisor" variable by one for every iteration of the loop.
Obviously, at some point of time, "divisor" will become zero. Anyattempt to continue division after this point will result in ugly C#error messages about illegal operations. This is where the "break"statement comes in - it can be used to exit the "for" loop as soon asthe value of the variable "divisor" becomes zero.
Should I pat myself on my back? Not really.
Take another look at the output and compare it against the code listing.You will see that the code exited the "for" loop before the conditionfor the loop became false.
Ideally, the script should exit the "for" loop when the value of the"count" variable becomes greater than 15, and, until then, shouldperform division for all values of the divisor other than 0 (includingnegative values).
In the example above, the "break" statement caused it to exit once thedivisor becomes 0 (on the tenth iteration of the loop). Therefore, onlypositive divisor values are taken care of by the code above; divisionoperations involving negative values of the divisor never even see thelight of day.
Is there a way to fix this? Sure, with the "continue" keyword. Take alook:
<script language="C#" runat="server">
void Page_Load()
{
// some variables to play with
int dividend = 7;
int divisor = 10;
// a for loop to perform division
for(int count = 1; count <= 15; count++)
{
// if divisor is zero
// decrement divisor and continue
if(divisor == 0)
{
divisor--;
continue;
}
// print loop counter and perform division
output.Text += "Loop counter is " + count + "." + " ";
output.Text += dividend + " / " + divisor + " = " +
((double)dividend/divisor) + "
";
divisor--;
}
output.Text += "<h3>All done, time to stop!</h3>";
}
</script>
<html>
<head></head>
<body>
<h3>Divide And Conquer</h3>
<asp:label id="output" runat="server" />
</body>
</html>
Here's the output:
Divide And Conquer
Loop counter is 1. 7/10 = 0.7
Loop counter is 2. 7/9 = 0.777777777777778
Loop counter is 3. 7/8 = 0.875
Loop counter is 4. 7/7 = 1
Loop counter is 5. 7/6 = 1.166666666666667
Loop counter is 6. 7/5 = 1.4
Loop counter is 7. 7/4 = 1.75
Loop counter is 8. 7/3 = 2.333333333333333
Loop counter is 9. 7/2 = 3.5
Loop counter is 10. 7/1 = 7
Loop counter is 12. 7/-1 = -7
Loop counter is 13. 7/-2 = -3.5
Loop counter is 14. 7/-3 = -2.3333333333333333
Loop counter is 15. 7/-1 = -1.75
All done, time to stop!
In this revised example, when the value of the "divisor" variablebecomes zero, I decrement its value and call the "continue" keyword.This "continue" keyword skips over the current iteration of the loop andsend control of the program back to the start of the "for" loop for thenext iteration (unlike the "break" statement, which simply exits theloop immediately). As a result, the script continues to iterate throughthe "for" loop until the conditional statement is no longer true, andthe value of the "count" variable becomes greater than 15.
For obvious reasons, there is no output for that particular iteration ofthe loop when the value of "divisor" variable is zero.
Next: End of Play >>
More ASP.NET Articles
More By Harish Kamath (c) Melonfire