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.
source share