Negate condition of an if-statement
scala
Solution
In Scala, you can check `if` two operands are equal (`==`) or not (`!=`) and it returns true if the condition is met, false if not (`else`).
if(x!=0) {
// if x is not equal to 0
} else {
// if x is equal to 0
}
By itself, `!` is called the Logical NOT Operator.
Use it to reverse the logical state of its operand.
If a condition is true then Logical NOT operator will make it false.
if(!(condition)) {
// condition not met
} else {
// condition met
}
Where `!(condition)` can be
- any (logical) expression
- eg: `x==0`
- -> `!(x==0)`
- any (boolean like) value
- eg: `someStr.isEmpty`
- -> `!someStr.isEmpty`
- no need redundant parentheses
Problem
If I want to do something like: ``` if (!(condition)) { } ``` What is the equivalent expression in Scala? Does it looks like? ``` if (not(condition)) { } ``` For example, in C++, I can do: ``` bool condition = (x > 0) if(!condition){ printf("x is not larger than zero") } ``` EDIT: I have realized that Scala can definitely do the same thing as I asked. Please go ahead and try it.