We have seen how you can use unary operators to increment and decrement variables. We can use these in conditions to allow us to update and test a variable at the same time:
count = 1 ;
if ( --count ) {
/* because count is made 0 */
/* before the test we never*/
/* get here */
}
count = 0 ;
if ( count-- ) {
/* because the 0 value of */
/* count is tested we */
/* never get here */
}
There is also a unary operator which uses the !
character. This is NOT and can be applied to a single operand or a logical expression in brackets. It does what it says, in that it inverts the bits and can therefore invert the sense of a condition.
/* Complex Expression */
void main ( void )
{
int count = 1 ;
if (count--)
{
/* get here if count is */
/* non-zero before it is */
/* decreased by 1 */
}
}