C++20 sections · 1024 units
Open in Course

Logical NOT

Flipping true and false

The ! operator inverts a bool value. if (!found) executes when found is false. This is clearer than writing if (found == false). You can negate entire expressions. if (!(x > 0 && x < 10)) is true when x is outside 1-9.

This equals if (x <= 0 || x >= 10) by De Morgan's law. Double negation cancels out. !!x converts any non-zero integer to true and zero to false. This is rarely needed but understanding it prevents confusion.