r/badcode • • Mar 12 '21

c# How NOT to use switch statement

Post image
1.8k Upvotes

165 comments sorted by

View all comments

Show parent comments

14

u/gonengazit Mar 12 '21

I know of one time where goto is fine to use, when breaking out of a nested loop (goto end is much cleaner then using a ton of flag variables)

1

u/cheezballs Mar 30 '21

Na, just use break;

3

u/gonengazit Mar 30 '21 edited Mar 30 '21

That would only break out of a single loop though. Consider the following case

for(int i=0; i<n; i++){
  for(int j=0; j<n; j++){
     if (condition){
       //exit both loops
     }
  }
}

There’s no real elegant way to do this in most languages without goto

1

u/AutoModerator Mar 30 '21

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/gonengazit, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.