0

I am working on a language where there is not any goto,jump function. For example, Matlab. Can you please help me on how to avoid using it? Is there any simple trick solving my problem?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Rizias
  • 211
  • 6
  • 12

3 Answers3

3

You should consider using break and continue

Instead of:

for ...
   ...
   if ...
      goto nextstuff:
   end
end
nextstuff:

You can do:

for ...
   ...
   if ...
      break
   end
end

And as @andrey said, you can often replace goto by if-else

Instead of doing:

if cond
  goto label
end
...
foobar()
...
label:
foobar2()

you can do:

if ~cond
  ...
  foobar()
  ...
end
foobar2()

When you use a goto to go back, you can replace it by a while:

Instead of doing:

redothat:
foobar()
...
if cond
   goto redothat;
end

you can do:

while cond
  foobar()
  ...
end
Oli
  • 15,935
  • 7
  • 50
  • 66
  • Thanks a lot for your reply and I am sorry for the wrong tag.My problem is that i want to implement a state diagram (trellis coding) and i have to go back in my code.I dont know if there is something obvious where i cant see (quite possible) but i cant find the solution. – Rizias Jan 24 '12 at 17:58
  • You should describe in more details your problem, if you want a less general answer... – Oli Jan 24 '12 at 18:04
  • I have a sequence of 100 random (binary) numbers. – Rizias Jan 24 '12 at 18:09
  • If you don't wna t a general answer, you should describe why (in your particular case) you think you need to use `goto`. I suggest you to post another question. – Oli Jan 24 '12 at 18:14
  • I am sorry for the 'I have a sequence of 100 random (binary) numbers' i wanted to add more but i pressed enter. – Rizias Jan 24 '12 at 18:22
1

Well, first of all you might as well ask it without the tag, you might get better answers. That is because this kind of question is common to almost all of the modern languages.

Instead of goto and jump you should use either conditionals like if,if-else or loops like while,for, depending on what you want to achieve.

Check out GOTO still considered harmful?, Why is goto poor practise?.

Community
  • 1
  • 1
Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
1

As @Andrey mention you can use if or if-else statement. In many cases loops like while, for is one-to-one replacements to the if-else and goto.

You should also consider to use break and continue statement as @Oli said above.

In some rare cases you can use exception (I don't know whether the Matlab supports it) in order to "go back". This is somewhat controversial, but maybe in your case it will fit.

redothat:
foobar()
...

And inside foobar() in some place you have

if cond
   goto redothat;
end

you can do:

while(true){
 try {
   foobar();
   ...
   break;
 }
 catch(YourApplicationException e){
   //do nothing, continiue looping
 }  
}

And inside foobar() in some place you have

if cond
  throw YourApplicationException();
end

Or you can do something like this:

you can do:

boolean isOk = false;   
while(! isOk){
 try {
   foobar();
   ...
   isOk=true;
 }
 catch(YourApplicationException e){
   //do nothing, continiue looping
 }  
}
alexsmail
  • 5,661
  • 7
  • 37
  • 57