How to make a return to the beginning of the program?

if (att == 1) {
Console.WriteLine ("You used normal attack");
Console.WriteLine ("Damage:" + attack);
Console.WriteLine ("enemy remaining:" + (ehp - attack) + "HP");
Console.WriteLine ("the Enemy you killed, try again")
(There should be a trivial restart of the program.)
}
June 14th 19 at 21:02
1 answer
June 14th 19 at 21:04
Solution
there are three options:
1) use a label
M:
if (att == 1) {
//or here M:
Console.WriteLine ("You used normal attack");
Console.WriteLine ("Damage:" + attack);
Console.WriteLine ("enemy remaining:" + (ehp - attack) + "HP");
Console.WriteLine ("the Enemy you killed, try again")
goto M; //(Here should be a trivial restart of the program.)
}

2) to make the feature separately (recursion)
private void MyFunc(bool stop = false;)
{
if(stop) return;
if (att == 1) { 
Console.WriteLine ("You used normal attack");
Console.WriteLine ("Damage:" + attack);
Console.WriteLine ("enemy remaining:" + (ehp - attack) + "HP");
Console.WriteLine ("the Enemy you killed, try again")
MyFunc(); //(Here should be a trivial restart of the program.)
}
}

3) use a for-loop (endless)
int damage = 100;
for(;;)
{
if (att == 1) { 
Console.WriteLine ("You used normal attack");
Console.WriteLine ("Damage:" + attack);
Console.WriteLine ("enemy remaining:" + (ehp - attack) + "HP");
Console.WriteLine ("the Enemy you killed, try again") 
}
hp < damage ? break : continue; //(Here should be a trivial restart of the program.)
}


upd
In their own way have thrown in some places the exit condition of the method/loop. Since your code is not clear, in any case, all this should cease, so that, when it must be finished - you adjust. Importantly, the above three ways, to fully answer your question.
Thank you, goto m most - anibal commented on June 14th 19 at 21:07
, goto - is a good thing, but for some reason (not sure why) guru programmers recommend to do without it. And perhaps it is not that goto somehow not working properly, but rather because the style of writing code following the OOP principles, MVVC, etc. goto I don't use anywhere else, and it turns out without it. Although, if need be, I will certainly use it if it will be convenient and easier. P. S. goto M - M, is a conditional variable, you can use any other letter: A, S, B, etc. - jaida.Dic commented on June 14th 19 at 21:10
About the fact that the conditional M I know, and thanks again) - anibal commented on June 14th 19 at 21:13
it would be strange if it would be possible to only ispolzovatj M) - anibal commented on June 14th 19 at 21:16
,
goto - is a good thing, but somehow [...] recommend to do without it

Usually goto complicates code. Especially if you goto from a loop outside. While, break and continue - that's another goto, but they are normal, because it is a goto to a certain point (the beginning or end of cycle).
Therefore, if you normally need a goto, it almost always means that the algorithm is not very good ("bad smells"). Better to alter the algorithm so that goto was not needed.
But in some cases a goto is needed. For example, in the switch.

I recommend not to use goto. Try to do. At least use the while (true) as you said . - caesar commented on June 14th 19 at 21:19

Find more questions by tags C#