Wednesday, 4 May 2016

Fixing C 2

Following on from the previous post.

Something I've always fancied to help handle error cases is for/else and while/else

for (int x = start_value; x > 0; --x) {
} else {

... and the same for while loops.

You see, the loop construct has to test the initial condition before retesting when the loop loops around. It would not be hard to add an "else" clause e.g. if the loop is never executed then please do this.

It would save the coder writing an additional "if" test and can trivially be used to catch errors e.g. what do you do (or not do) in the above case if start_value is 0.

No comments: