Are there any common uses of C# do-while loop in web development? [closed]
I've been coding for a long, long time, self-taught. Now that I'm working toward my computer science degree, my programming class is talking about post-test loops, the do-while loop.
Now I understand that it runs once, then checks the condition... since I'm a web programmer, I was wondering: what are开发者_开发百科 some common web development (ASP.NET MVC/Razor preferably) scenarios where a do-while loop fits perfectly?
I'm not quite getting the concept straight in my mind...
I have been involved in many web projects and I can't remember for any specific use for this type of a loop. On web, you usually work just with collection of items and do some logic with each one of them. So I would say that most web developers just use foreach
and for
.
Do - While fits more into algorithms and calculations, and you don't do many of these in classic web development imo.
I don't know about MVC specifically, but a couple of years ago I had to create a administration page to enter data, on the client side you could add new fields to add staff member profiles. (name, title, education)
The controls were created client-side with JavaScript and numbered in sequence like:
txtStaffName0
txtStaffName1
txtStaffName2
Then used a do/while with the condition checking for the next incremented number for a control. Something along the lines of:
int count = 0;
do
{
//find control with count and do stuff...
count++;
} while (DoesControlExist(count));
Apart from that I don't think I've ever seen do/while used in web related stuff...
As most of the interface logic has been implemented at client side (to save bandwidth and for performance reasons), so this loop has almost no need . Also the difference between program flow in a web and console (or windows application) is that web page run in a context for many users while windows application is used mostly by a single user where user can have option to manage control flow like continue or exit.
I bet the syntax reveal it all, if having a situation the you need to perform first before checking.
do {
// Do your algo first.
} while (true); // Check later.
Now in real code, I can assume:
int x = 100;
do {
x = y - 10;
} while (x >= 0);
Its like to concept of x++
and ++x
inside the for loop of C, it makes you decide to increment now or later.
Based on my experience usage of loops varies as per requirment.
If you iterate through a collection and do some action for each object in a collection, for each would be safe from run-time exception.
But for each doesn't work if you want to remove an item from collection based on a condition, there for loop is the best one. do from collection.count() - 1 to 0.
Usual for loop is used to execute some action or calculation.
Personally i rarely use do while.
I very nearly asked this exact question yesterday. In all my years of web development, I can't think of a single time I've used a do/while in production code. On the other hand, I hardly ever use recursion either, but on those few occasions when I want it, it's good to have.
Anyway, this article suggests using it in a console app to show a menu and redraw it after every input. Here's an edited snippet of the article's code:
string myChoice;
do
{
// Print A Menu
Console.WriteLine("My Address Book\n");
Console.WriteLine("A - Add New Address");
Console.WriteLine("D - Delete Address");
Console.WriteLine("Q - Quit\n");
Console.WriteLine("Choice (A,D,M,V,or Q): ");
// Retrieve the user's choice
myChoice = Console.ReadLine();
// Make a decision based on the user's choice
switch(myChoice)
{
case "A":
Console.WriteLine("You wish to add an address.");
break;
case "D":
Console.WriteLine("You wish to delete an address.");
break;
case "Q":
Console.WriteLine("Bye.");
break;
}
Console.WriteLine();
} while (myChoice != "Q"); // Keep going until the user wants to quit
This seems reasonably useful. You could, of course, get around it by setting the initial value of myChoice
to something other than "Q", or by adding a bool to the while
condition, or something like that, but do/while gets the job done marginally easier. It's not essential like recursion is; I don't think there's much (if anything) that do/while does significantly better than while. But it's a holdover from C and it's occasionally useful, so there's no reason to get rid of it.
精彩评论