|
-
January 28th, 2010, 01:50 AM
#1
C# good alternatives to C++/goto statement

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
-
January 28th, 2010, 11:22 AM
#2
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'.
-
January 28th, 2010, 11:35 AM
#3
Re: C# good alternatives to C++/goto statement
 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.
===============================
My Blog
-
January 28th, 2010, 01:59 PM
#4
Re: C# good alternatives to C++/goto statement
 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.
-
January 28th, 2010, 02:03 PM
#5
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.
-
January 28th, 2010, 06:37 PM
#6
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!
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
January 28th, 2010, 06:53 PM
#7
Re: C# good alternatives to C++/goto statement
 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.
-
January 29th, 2010, 02:15 AM
#8
Re: C# good alternatives to C++/goto statement
using while loops along with continue and break statements would solve your problem i suppose..
-
January 29th, 2010, 03:15 AM
#9
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.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
-
January 29th, 2010, 06:39 PM
#10
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.
-
January 29th, 2010, 08:07 PM
#11
Re: C# good alternatives to C++/goto statement
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
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
February 5th, 2010, 11:51 PM
#12
Re: C# good alternatives to C++/goto statement
continue:

break:
Last edited by jonlist; February 5th, 2010 at 11:53 PM.
Reason: mixed up
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|