C GOTO STATEMENT

Locked. This question và its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
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.

Bạn đang xem: C goto statement

Summary (label changed from original lớn make intent even clearer):

infinite_loop: // code goes heregolớn infinite_loop;Why it"s better than the alternatives:

It"s specific. golớn is thelanguage construct which causes anunconditional branch. Alternativesdepend on using structuressupporting conditional branches,with a degenerate always-truecondition.The label documents the intentwithout extra comments.The reader doesn"t have sầu khổng lồ scan theintervening code for early breaks(although it"s still possible for anunprincipled hacker khổng lồ simulatecontinue with an early goto).

Rules:

Pretend that the gotophobes didn"twin. It"s understood that the abovecan"t be used in real code becauseit goes against established idiom.Assume that we have sầu all heard of"Golớn considered harmful" và knowthat golớn can be used lớn writespaghetti code.If you disagree with an example,criticize it on technical meritalone ("Because people don"t likegoto" is not a technical reason).

Xem thêm: Chủ Đề 8: Cụm Động Từ Với Set For Là Gì ? Nghĩa Của Từ Set Trong Tiếng Việt

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 lớn everyone, especially those who took my little loop example seriously. Most skeptics were concerned by the lack of bloông chồng scope. As
quinmars pointed out in a comment, you can always put braces around the loop toàn thân. I note in passing that for(;;) and while(true) don"t give sầu you the braces for không lấy phí either (& omitting them can cause vexing bugs). Anyway, I won"t waste any moreof your brain power on this trifle - I can live with the harmless & idiomatic for(;;) và while(true) (just as well if I want to keep my job).

Considering the other responses, I see that many people view goto as something you always have khổng lồ rewrite in another way. Of course you can avoid a goto lớn by introducing a loop, an extra flag, a stachồng of nested ifs, or whatever, but why not consider whether goto is perhaps the best tool for the job? Put another way, how much ugliness are people prepared to endure khổng lồ avoid using a built-in language feature for its intended purpose? My take is thateven adding a flag is too high a price khổng lồ pay. I like my variables to represent things in the problem or solution domains. "Solely khổng lồ avoid a goto" doesn"t cut it.

I"ll accept the first answer which gave the C pattern for branching lớn a cleanup bloông chồng. IMO, this makes the strongest case for a goto lớn of all the posted answers, certainly if you measure it by the contortions a hater has to go through to lớn avoid it.