KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
In this thread, we look at examples of good uses of goto in C or C++. It's inspired by an answer which people voted up because they thought I was joking. Summary (label changed from original to make intent even clearer): infinite_loop: // code goes here goto infinite_loop; Why it's better than the alternatives: It's specific. goto is the language construct which causes an unconditional branch. Alternatives depend on using structures supporting conditional branches, with a degenerate always-true condition. The label documents the intent without extra comments. The reader doesn't have to scan the intervening code for early break s (although it's still possible for an unprincipled hacker to simulate continue with an early goto ). Rules: Pretend that the gotophobes didn't win. It's understood that the above can't be used in real code because it goes against established idiom. Assume that we have all heard of 'Goto considered harmful' and know that goto can be used to write spaghetti code. If you disagree with an example, criticize it on technical merit alone ('Because people don't like goto' is not a technical reason). Let's see if we can talk about this like grown ups. Edit This question seems finished now. It generated some high quality answers. Thanks to everyone, especially those who took my little loop example seriously. Most skeptics were concerned by the lack of block scope. As @quinmars pointed out in a comment, you can always put braces around the loop body. I note in passing that for(;;) and while(true) don't give you the braces for free either (and omitting
Tags (comma-separated)
Save Edits
Cancel