< Previous | Contents | Next >
The logical NOT operator, !, lets you switch the truth or falsity of an expression. The new expression is true if the original is false; the new expression is false if the original is true. Just as in English, “not” means the opposite. The new expression has the opposite value of the original.
I use the NOT operator in the Boolean expression of the do loop:
} while (!success);
The expression !success is true when success is false. That works perfectly because success is false only when there has been a failed login. In that case,
the block associated with the do loop executes again and the user is asked for his username and password once more.
The expression !success is false when success is true. That works perfectly because when success is true, the user has successfully logged in and the loop ends.
false
true
true
false
!security
security
Table 2.5 Possible Login Combinations Using
the NOT Operator
Another way to understand how ! works is to look at all of the possible combinations of truth and falsity (see Table 2.5).