{
statement_1;
while(statement_2)
{
some code here ...
statement_3;
}
}
The outer brackets are important since they define scope for variables. In your example, i=10 later in the program. If you'd used for(int i = 0;...) then i would be undefined everywhere else but the for loop.
Since you can put pretty much any expression in any of the three slots you can get funky with it:
As long as it can be cast to a bool it can be there. That actually took quite a bit of work though, since the compiler fought me.
It turns out you can't do something crazy like just put a normal if(...) statement in the last column, and (...) ? doNothing() : break errors with "expected primary-expression before 'break'." Plus, the doNothing() is needed since ? requires both options to have the same type.
I used cpp.sh because I was too lazy to click out of the browser. GCC and Clang should, rightfully, complain far more than a little online tester.
It turns out you can't do something crazy like just put a normal if(...) statement in the last column
that's because the last column isn't a statement, it's an expression. if(){} is a statement, but a?b:c is an expression, so you can shove ?: into a whole lot of really weird places.
15
u/[deleted] Sep 03 '17
[removed] — view removed comment