2
$\begingroup$

What is the best way to avoid using goto statements when writing the pseudocode for an algorithm?

I want to avoid it and use another way to replace it. enter image description here

$\endgroup$
5
  • $\begingroup$ That’s a while(true) loop, with a continue statement inside the if. Isn’t it? $\endgroup$
    – gigabytes
    Jul 13 '19 at 6:53
  • 2
    $\begingroup$ Why do you not want to use goto in your pseudocode? The point of pseudocode is to be readable as easily as possible. If goto helps with that (since apparently you can't even think of a simple way to write the code without goto), why would you not want to use goto? $\endgroup$ Jul 14 '19 at 8:51
  • 1
    $\begingroup$ What does $\mbox{close}$ mean? If it's a statement, it is never reached. $\endgroup$ Aug 13 '19 at 9:38
  • $\begingroup$ That code can only return false or loop forever. Is that really the intended behaviour? But, to answer your question, you get rid of goto statements in pseudocode in the same way as you would in any actual language. $\endgroup$ Aug 13 '19 at 10:46
  • $\begingroup$ this close statement is rather strange, When will it executed (never?) what is closed? Nothing was opened. $\endgroup$
    – miracle173
    Aug 13 '19 at 17:27
3
$\begingroup$

In many cases, GOTO statement can be replaced by loops. For example, in your case, the inner loop is just "while string(i) = path(j), decrease both i and j", and the outer loop is a "loop forever", also known by the C idiom "while true".

$\endgroup$
0
$\begingroup$

Whoever wrote that pseudo code decided to do flow control in the simplest possible way: Using labels and "goto". You'd have to use a different model for pseudocode, for example one which uses loops and "break" statements. Now you made your pseudocode "language" more complicated.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.