Putting a composite statement in the condition of a for loop

前端 未结 6 1081
一整个雨季
一整个雨季 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:34

    For both readability and performance, I think the following code can be used:

    for (ii = 0;; ii++)
    {
        printf("%d", ii);
        (ii < 3) ? (putchar(' ')) : ({ putchar('\n'); break; });
    }
    

    Actually, the above code is similar to your last code. It is readable, and it still has one conditional evaluation in each increment.

提交回复
热议问题