C # goto use - what else to use here?

I know that using goto is what most people say to avoid, however I read in different places, which is sometimes useful if you need simple code. Currently, I have a very simple program that needs to be repeated if the user chooses this:

static void Main() { Restart: ... string UserChoice=Console.ReadLine(); if (UserChoice=="y") goto Restart; } 

Is using goto here really that bad? I just don't see another way to repeat the code without doing loops, etc. This seems like a very simple and clean way. Or am I missing something?

+6
syntax c # goto
source share
11 answers
 string userchoice; do { userchoice=Console.ReadLine(); } while (userchoice=="y"); 
+20
source share

instead, write something like below.

 while(Console.ReadLine() == "y") { ..... } 

Yes, using goto is bad because it makes your code less readable.

+5
source share

Of course, if your code does the same thing over and over, you need to add a loop. This is much better than going over.

Use something like this

 string UserChoice = "y"; while( UserChoice == "y"){ ... UserChoice=Console.ReadLine(); } 

This should work well for you.

+5
source share

Is using goto here really that bad?

In March 1968, Dijkstra sent an email to ACM Communications , which was published under the heading Go to application is considered malicious . This is an interesting read and part of the programmer’s knowledge.

The argument against GOTO presented in this letter is about how programmers create a mental model to track the progress of code. Dijkstra argues that such a mental model is important because the meaning of variables only makes sense regarding progress. For example, when our program counts the number of times when an event occurs, there is always an intermediate moment when N events have occurred, but the variable tracking it has not yet been increased and is still at N-1.

He goes through these steps in his arguments against GOTO:

  • First, consider a very simple language without procedures, loops, or GOTO. In such a language, a programmer can mentally track execution, imagining that the progress bar moves from the beginning of the file to the end. To simulate execution, a single index is sufficient (i.e., Line numbers).

  • Now we are adding procedures to the language. Progress can no longer be tracked by a single index, since it can be inside a procedure. We also need to keep track of which line the procedure is called from. In addition, procedures can be called from other procedures. Therefore, we model the progress as a sequence of indices. (In real life, programmers call this sequence a "stack trace.")

  • Now add loops to the language. For each row in our stack trace that is inside the loop body, we need to add another type of index to execute the model: count repetition.

  • Now add GOTO. Dijkstra claims that with the unbridled use of GOTO, our ability to track progress is now breaking down. We can still track the progress with the "hours of execution", saying "now we are executing the 152nd statement." However, this is not very useful for determining the context that is needed to interpret the values ​​of variables.

As long as we use only GOTO instructions to create simple loops, you can argue that the situation is equivalent to point (3) and there is no problem. But in this case, you can just use loop constructs. It is better to simply leave GOTO outside your code so that you do not slip into the situation described in paragraph (4).

+4
source share

I would use a do / while loop:

 string UserChoice = ""; do { ... UserChoice=Console.ReadLine(); } while(UserChoice == "y"); 
+3
source share

You can use a recursive function to do the same without loops:

 public static void Main(string[] args) { PrintChoices(); } private static void PrintChoices() { string userChoice = Console.ReadLine(); if (userChoice == "y") PrintChoices(); } 
+2
source share

Using methods instead of GOTO is more widely accepted:

 static void Main() { Restart(); } static void Restart() { ...code string userChoice = Console.ReadLine(); if (userChoice=="y") Restart(); } 
+2
source share

The answers are missing one basic solution,

 while (true) { ... if (other-stop-condition) break; ... string UserChoice=Console.ReadLine(); if (UserChoice != "y") break; } 

The break statement is considered less structured than pure, but more structured than goto (real). It should be used sparingly, but it has its uses, as with other-stop-condition

uses goto here really so bad?

Not in this simple program. But if you continue to use goto to replace loops, if / then etc. Your code will increase in complexity much faster than code that avoids goto .

+2
source share

Use the do while loop to replace your goto as its more readable.

 do { ... } while(Console.ReadLine() == "y"); 
+1
source share

Personally, I never had to use goto, and, for example, Øyvind Bråthen and Numenor stated that the loop method is the best way to accomplish this task.

However, there is one case where I can think of where goto would be useful

How a switch "fails" in C # is illegal (causes a compiler error):

 switch (a) { case 3: b = 7; case 4: c = 3; break; default: b = 2; c = 4; break; } 

To make it work, you can use goto:

 switch (a) { case 3: b = 7; goto case 4; case 4: c = 3; break; default: b = 2; c = 4; break; } 
0
source share
 class Program { static void Main(string[] args) { int add; try { ArrayList al = new ArrayList(); t: Console.Write("Enter the Number of elements do you want to insert in arraylist"); int n = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the Values:"); for (int i = 0; i < n; i++) { al.Add(Console.ReadLine()); } foreach (var a in al) { Console.WriteLine("valus:" + a); } Console.WriteLine("Add another number yes:1 "); add = Convert.ToInt32(Console.ReadLine()); while (add == 1) { goto t; } } catch (Exception ex) { Console.WriteLine("Enter the Valid Number and try again"); } Console.ReadKey(); } } 

}

0
source share

All Articles