Sujet : Re: do { quit; } else { }
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.cDate : 06. Apr 2025, 11:05:40
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vstjlk$mis5$2@dont-email.me>
References : 1
User-Agent : Mozilla Thunderbird
On 04/04/2025 21:23, Thiago Adams wrote:
What do you think of this control block?
do
{
FILE f = fopen("file.txt", "r");
if (f == NULL) quit; /*goes to else part*/
/*success here*/
for (int i =0; i < 10; i++){
...
if (error) quit;
}
}
else
{
/*some error*/
}
This does not strike me as worth the effort as a control block. There are always vast numbers of ways to make different kinds of control blocks in a language - the trick is to pick enough to be able to write code conveniently, and not so many that it is hard to learn the language and understand the code.
Modern languages often have some kind of try/except structure, which is what this looks like. But I don't think that is a good idea to try to bolt onto a C compiler as an extension - it would give little benefit and a lot of incompatibility. It is best done as a more fundamental part of a language design. Without integration in the library, all you have is syntactic sugar for a "goto" without even saving much typing.
Now, if you were to invent a do / undo control block, where the "undo" statement not merely exits the control block but undoes the effect of everything done so far inside the block, /then/ you would have something new and exciting!