0
0
0000f2020-10-21 11:17:59
C++ / C#
0000f, 2020-10-21 11:17:59

Guys, what kind of loop is better to use when entering data?

I am learning and still can not decide which one is better to use, it will be useful to learn from you

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
cicatrix, 2020-10-21
@cicatrix

Any finite loop on machine instructions looks like a condition check + conditional jump.
In general, the same goto wrapped in a beautiful wrapper.
So for the processor there is no difference. It is for the person who will read/maintain the program, so write the code as if it would be accompanied by a bloodthirsty psychopathic maniac who knows where you live.
Make your code clear and readable.

F
freeExec, 2020-10-21
@freeExec

No cycles needed, goto better .

V
Vladimir Korotenko, 2020-10-21
@firedragon

var ret = true;
                    do
                    {
                        var key = Console.ReadKey().KeyChar;
                        if (key == 'a')
                        {
                            _replaceMode = ReplaceMode.Override;
                            ret = false;
                        }
                        if (key == 'b')
                        {
                            throw new ArgumentException($"File exist, breaking:  {newWikiPageUrl}");
                        }
                        if (key == 'r')
                        {
                            ret = false;
                        }
                    } while (ret);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question