Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
ASP.NET Basics (part 4): Looping The Loop
Control the flow of your ASP.NET scripts with loops.

| Counting Down |

The most basic loop available in C# is the "while" loop, and it looks like this:


while (condition)
{
do this!
}


Or, to make the concept clearer,


while (rich Uncle Ed's still breathing)
{
be nice to him
}


The "condition" here is a standard C# conditional expression, which evaluates to either true or false. So, were you told to write the above example in C#, it would look like this:


while (rich_old_guy_lives == 1)
{
be_nice();
}


The "while" control structure tests the conditional expression first, and only proceeds to execute the statements within the loop if the expression evaluates to true.

How about an example to demonstrate the "while" loop?


<script language="C#" runat="server">
void Page_Load()

// define a variable for the countdown
int countdown = 10;

output.Text = "Beginning countdown...";

// repeat the statement so long as the
// variable is greater than or equal
// to zero
while(countdown >= 0)
      {
output.Text += "<br>" + countdown;
countdown--;
}

output.Text += "<h1>Houston, we have lift-off!</h1>";

}
</script>
<html>
<head><title>Countdown To Launch</title></head>
<body>
<asp:label id="output" runat="server" />
</body>
</html>


Here is the output.

Output image

Each loop requires a variable, or "loop counter", to track the number of iterations. In the example above, this variable is called "countdown". I have initialized it to 10, since the loop will start counting down from that value.

Next, we have the "while" loop proper:


<%
while(countdown >= 0)
      {
output.Text += "<br>" + countdown;
countdown--;
}
%>


This is pretty straightforward stuff. The "while" statement will check if the value of the "countdown" variable is greater than or equal to zero. If it is, it will enter the loop and execute the enclosed statements (in this case, print the value of the variable followed by a line break). It then decrements the value of the "countdown" variable by one (using the -- operator discussed previously), and repeats the procedure.

On the eleventh iteration, the value of the countdown variable will be -1. Therefore, the result of the conditional statement will be false, the loop will be exited and the statements following the loop will be processed.

What if you forget to decrement the value of the loop counter (a common newbie mistake)? Start praying, and flip the page.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback