3 ms·
This is absolutely true. Even advanced loop syntax implies nesting; but control flow in general is not hierarchical. Simple loop syntax in C is even more limite
by Mikhail_Edoshin 2y ago
This is absolutely true. Even advanced loop syntax implies nesting; but control flow in general is not hierarchical. Simple loop syntax in C is even more limited, because it uses a fixed place for the loop condition test: either at the start or at the end of the body. As a result if we need to do something, then test the condition, then do something extra, we are stuck. For example, we may want to print a list of stings separated with commas:
void printManyStrsWithCommas(char *s[], int n /* trusted to be positive */)
{
int i = 0;
a: printOneStr(s[i]);
++i;
if (i < n) {
printOneStr(", ");
goto a;
}
}
Here the loop condition is 'i < n', but when it is true, we need to do one more thing (print a comma) before resuming the loop. If we are to stick with non-'goto' syntax we have the following options:
- Somehow cram it into the loop expression with the comma operator. This is very limited because it is an expression. For example, what if 'printOneString' can err?
- Somehow use 'while(1)' with 'break'. It may even compile to the same code as with 'goto'. But why, then, we have to use an unbounded loop syntax with a clearly bounded sequence?
- Somehow add additional variables and tests and either lose efficiency or trust the compiler to lead us out.
With 'goto' the code does only what is necessary. What 'goto' does not do is that it does not immediately convey a clear idea that the code repeats certain steps.
- trealira 2y agoIn this particular case I think you could do one of the following (ignoring the "while (true)" + "break" combination): 1: while (printOneStr(s[i]), ++i < n) printOneStr(", "); 2: goto start; do { printOneStr(", "); start: printOneStr(s[i]); ++i; } while (i < n); But for this kind of code in general, I have thought that the "do while" statement should be extended to take an extra statement after the "while (cond)" part. It could still be an empty statement, with just a semicolon, like "do ... while (cond);", exactly like it's already used; but in cases like yours, you would be able to write this: do { printOneStr(s[i]); ++i; } while (i < n) { printOneStr(", "); } As a bonus, the current mandatory semicolon after do-while statements would stop being a special case for the grammar of C.