The 'not' Operator
Part of the Fundamentals section of Coddy's Lua journey. Lesson 22 of 90.
not operator, which works differently from and and or. Instead of combining two conditions, the not operator inverts a single boolean value, flipping true to false and false to true.
Here's how the not operator works:
gameOver = false
gameRunning = not gameOver -- gameRunning is true
playerAlive = true
playerDead = not playerAlive -- playerDead is falseThe not operator is particularly useful when you need to check the opposite of a condition. For example, instead of checking if a game is over, you might want to check if it's not over to continue playing.
isComplete = false
canContinue = not isComplete -- canContinue is true
print(canContinue) -- Output: trueChallenge
EasyCheck if a game is currently running by using the not operator to invert a game over status. Create a variable called gameOver and assign it the value false. Use the not operator to create a new variable called gameRunning that represents the opposite of the game over status. Print the result.
Try it yourself
-- TODO: Write your code here
-- Print the result
print(gameRunning)This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators 2 Relational & Logic
Equality OperatorsRelational OperatorsThe 'and' OperatorThe 'or' OperatorThe 'not' OperatorShort-Circuit EvaluationTruthy and Falsy ValuesRecap - Simple Logic7Basic Conditional Logic
The if-then StatementThe if-then-else StatementThe elseif StatementRecap - Treasure Chest2Variables and Data Types
What is a Variable?NumbersStringsBooleansThe Value 'nil'The type() FunctionNaming ConventionsRecap - Character Profile5Basic Output
Printing LiteralsPrinting VariablesPrinting Multiple ValuesCombining Strings & VariablesThe tostring() FunctionInputCastRecap - Status ReportRecap - Till 1208String Manipulation Basics
string.len()string.upper & string.lowerstring.sub()string.rep()string.find()Recap - Format Username3Operators 1 Arithmetic & Conc
Arithmetic OperatorsModulo OperatorExponentiation OperatorString ConcatenationOperator PrecedenceRecap - Simple Calculations6Project: Character Stats Disp
Welcome MessageDeclare Character Stats9Functions Basics
Declaring a FunctionCalling a FunctionFunctions with ParametersFunctions with Multiple ParamsThe 'return' StatementRecap - Area CalculatorPractice on your own: Online Lua compiler