Putting a composite statement in the condition of a for loop

前端 未结 6 1150
一整个雨季
一整个雨季 2021-01-18 18:02

I have a contrived example to demonstrate the request for a specific functionality - I wonder if anyone has a clever trick to do this.

The following is a problem one

6条回答
  •  天命终不由人
    2021-01-18 18:36

    [In many ways this question should be closed as it's opinion based.]

    This problem crops up often. I always opt for a solution that minimises the instructions in the iterative part.

    { /*don't pollute the outer scope with ii*/
        int ii;
        for (ii = 0; ii < 3; ++ii/*I've always preferred this to ii++*/) {
            printf("%d ", ii);
        }
        printf("%d\n", ii);
    }
    

    Ternaries, if statements etc. just obfuscate things. In my opinion.

提交回复
热议问题