Are there any general uses of the C # do-while loop in web development?

I have been programming for a long time, studying. Now that I am working on a computer science degree, my programming class is talking about post-test loops, a do-while loop.

Now I understand that it starts once, and then checks the condition ... since I am a programmer on a website, I was wondering: what are the usual web development (ASP.NET MVC / Razor desirable) scripts, and the loop is perfect?

I do not quite understand this concept ...

+6
source share
6 answers

I have participated in many web projects and I cannot remember for any specific use for this type of loop. On the Internet, you usually only work with a collection of items and perform some logic with each of them. Therefore, I would say that most web developers just use foreach and for .

Do - Although this is more suitable for algorithms and computations, and you do not do many of them in the classic imo web development.

+3
source

I donโ€™t know about MVC on purpose, but a couple of years ago I had to create an administration page for entering data, on the client side you could add new fields for adding employee profiles. (name, title, education)

The controls were created on the client side with JavaScript and numbered in the following order:

txtStaffName0

txtStaffName1

txtStaffName2

The do / while command is then used to check the condition for the next incrementing number for the control. Sort of:

 int count = 0; do { //find control with count and do stuff... count++; } while (DoesControlExist(count)); 

Also, I don't think I've ever seen what I am doing / using in web content ...

+1
source

Since most of the interface logic is implemented on the client side (to save bandwidth and for performance reasons), this cycle is practically unnecessary. Also, the difference between the program flow on the network and the console (or the Windows application) is that the web page is executed in the context for many users, while the Windows application is used mainly by one user, where the user can be able to control the control flow. for example, continue or exit.

+1
source

I'm sure the syntax reveals all of this, if you have a situation, you need to complete it 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); 

Similar to the concept of x++ and ++x inside a for C loop, this forces you to decide whether to increase now or later.

0
source

Based on my experience, the use of loops varies according to requirements.

If you iterate over a collection and perform some actions for each object in the collection, a run-time exception will be safe for each of them.

But for everyone it doesnโ€™t work, if you want to remove an item from the collection based on the condition, then for the loop the best. do from collection.count () - from 1 to 0.

A regular loop is used to perform some action or calculation.

Personally, I rarely use do while.

0
source

I very precisely asked this exact question yesterday. For all the years of developing web applications, I canโ€™t think that I used do / while code in production code. On the other hand, I almost never use recursion, but in the few cases where I want it, itโ€™s good to have one.

In any case , this article suggests using it in a console application to show the menu and redraw it after each input. Here's an edited snippet of article 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 choice myChoice = Console.ReadLine(); // Make a decision based on the user 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 helpful. You could, of course, get around this by setting the initial value of myChoice to something other than "Q", or by adding bool to the while condition or something like that, but doing / while doing the job is made a little easier. This is not as important as recursion; I donโ€™t think that there are many (if you like) who do / do much better than now. But this is a delay from C, and it is sometimes useful, so there is no reason to get rid of it.

0
source

Source: https://habr.com/ru/post/886939/


All Articles