Liste des Groupes | Revenir à cl c |
On 07/12/2024 11:34, Janis Papanagnou wrote:Correction: the versions I tested don't have that 'do' in the middle. The two examples need to be written like this to be valid:On 06.12.2024 02:20, Bart wrote:Is it common to both start and end a statememt with the same keyword?I used to be puzzled by this too: 'while' can both start a while>
statement, and it can delimit a do-while statement. How is that possible?
A keyword (like 'while') is just a lexical token that can be used in
different syntactical contexts.
(Even with different semantics, if alanguage designer thinks this is a good idea for his case.)Now you are demonstrating my point about being treated like a beginner. And it is exasperating.
>
You may be less confused with using Pascal;
This is a point of language design. C's approach works - just. But it relies on some subtlety. 'while (cond)' both starts a statement, and can end a statement:
do while(cond) do ; while (cond);
The second 'while' here starts a nested while-loop inside a do-while loop. Not confusing? It is to me! I couldn't finish my example as I got lost (but as it happens, this is valid code, partly by luck).
Of course, if you put aside all other matters and concentrate on the minutiae of the syntax details, then it is all 'obvious'. I think it needs to be a lot more obvious.
while positive-case do ...The problem here is exactly the same: how do you tell whether the 'while' in 'do ... while' is ending this 'do'-loop, or starting a nested while-loop?
until negative-case do ...
do ... while positive-case
do ... until negative-case
(depending on language with this of similar syntax).
>
There's nothing wrong using the same keyword for the same type of
condition, rather it's good to have it defined that way.
You don't think there is an element of ambiguity here?
Let's take my example again:
do while(cond) do ; while (cond);
And make a small tweak:
do ; while(cond) do ; while (cond);
It still compiles, it is still valid code. But now it does something utterly different. (One is two nested loops, the other is two consecutive loops - I think!)
Les messages affichés proviennent d'usenet.