C# good alternatives to C++/goto statement
http://img686.imageshack.us/img686/537/zzzcz.jpg
Hi, I have a sample program structure in C++ as shown in the image link above. Anybody could suggest me a way to rewrite that structure into an equivalent in C# ?
As you can see from the picture, the C++ code use "goto" statement to jump around but I would not want to do the same in C#, I would want to avoid those "goto"s.
In fact:
1. I can't simply rewrite using the same "goto" as in C#, goto has more limited scope than in C++. It is not allowed to jump from a code block into another block in C# like we normally do in C++. in C#, label has its scope only in the code block that it's declared.
2. I find it hard to use switch or probably try/catch/finally since the original code uses goto to jump from outter code block to inner code block or vice versa. All those variables declared in the outter code block will not be visible to the inner one so the compiler will complain.
Anybody encounter this before, could suggest me a best practice to archive the same thing in C# ?
The worse case is that I may need to rewrite duplicate code to avoid "goto" ?
Thanks and pardon my English. English is not my native language.
Alex To
Re: C# good alternatives to C++/goto statement
Hi Alex To,
Your English is very good. I didn't see any image link. YOu may need to edit your post and sort that out. I think this is an opportunity to re-write the code in the C# style (Many would say that the over-use of the 'goto' is not good C++ either). You're not copying the code think of it as re-writing it but with the benefit of something to compare against or aim at. I would even say just forget about the 'goto' and any possible equivalents in C#. You should not need to duplicate code to avoid using the 'goto'. I've said all this without looking at any of the C++ code though...But I'm convinced you can re-write it in C# without the 'goto'.
Re: C# good alternatives to C++/goto statement
Quote:
Originally Posted by
nelo
Hi Alex To,
Your English is very good. I didn't see any image link. YOu may need to edit your post and sort that out. I think this is an opportunity to re-write the code in the C# style (Many would say that the over-use of the 'goto' is not good C++ either). You're not copying the code think of it as re-writing it but with the benefit of something to compare against or aim at. I would even say just forget about the 'goto' and any possible equivalents in C#. You should not need to duplicate code to avoid using the 'goto'. I've said all this without looking at any of the C++ code though...But I'm convinced you can re-write it in C# without the 'goto'.
the link to the image is at the top of the post, before he even starts his post.
As for alternatives, most use "while" or "do/while" loops. To exit a for loop, you can use the "break" keyword.
Re: C# good alternatives to C++/goto statement
Quote:
Originally Posted by
nelo
I've said all this without looking at any of the C++ code though...But I'm convinced you can re-write it in C# without the 'goto'.
You can also rewrite it in C++ without the goto as well.
Re: C# good alternatives to C++/goto statement
goto is only useful in some very specific situations. It looks to me as if your code just needs a few if statements inside of the inner loop and a 'break;' at the end of each one. Just remember that 'break' only returns you to the next block, one alone will not exit all three loops.
Re: C# good alternatives to C++/goto statement
Consider using 'break' and 'continue' statements in both C# and C++. However, make it very clear what is happening. If the loops contain a lot of code and you are using 'break' and 'continue' statements, consider moving some of the code to a seperate function, so the meaning of the 'break' and 'continue' statements is obvious.
Goto is a last resort. The last time I used Goto was in Commodore PET BASIC in 1982!
Re: C# good alternatives to C++/goto statement
Quote:
Originally Posted by
rliq
The last time I used Goto was in Commodore PET BASIC in 1982!
I know this sounds a bit snooty, but I agree. I haven't run into a case in C++ or C# where a goto was necessary.
Re: C# good alternatives to C++/goto statement
using while loops along with continue and break statements would solve your problem i suppose..
Re: C# good alternatives to C++/goto statement
I can back this. If I don't count Basic on my first Atari, I've used goto only once - and it survives in the code for about 30 minutes :) I cannot remebeer a situation where goto had been a "silver bullet". I can imagine it is useful in procedural programing, but in OOP I considere it obsolete. Despite that, If you need it, it still there with us.
Re: C# good alternatives to C++/goto statement
I agree with the previous posts; I have not needed to use goto in well over a decade of programming (not since BASIC on DOS).
You could use break/continue as the pp's suggested or split code out into logical functions and call the functions in your conditional statements.
Re: C# good alternatives to C++/goto statement
Quote:
I can back this. If I don't count Basic on my first Atari
Wow! Seems like we need a PET/Atari forum on here. Those were the days, when block graphics was state of the art and your application either went Beep or it didn't go Beep ;)
Re: C# good alternatives to C++/goto statement