Ad

Is There A Way To Represent Logical Not With && Or ||

- 1 answer

Is there a way to represent logical not, with && or || or some statement like if or ?:

I want to implement this in some other way:

boolean isRunning = true;
startButton.setEnabled(!isRunning); // <<== ???
Ad

Answer

Assuming that you are doing this as an exercise, ternary operator lets you replace ! in a simple and straightforward way:

startButton.setEnabled(isRunning ? false : true);

As far as using && and || by themselves goes, this pair of operators is not functionally complete, i.e. there are operations that cannot be implemented by using a sequence of &&s and ||s alone; not ! operation is among operations that cannot be implemented with ands and ors.

Ad
source: stackoverflow.com
Ad