Updated April 19, 2023
Definition of Lua Comment
The Lua comment is a function useful for information of the source code and does not execute in the Lua interpreter. It is an operator used to ignore unwanted programming and information of the primary source code from the Lua IDE. It helps to understand the main source code and provides explanations or information about the code without execution. It is an operator used to provide readable information about code in the text editor but does not execute from the Lua compiler and Lua interpreter. It is small text or information used for understanding critical code or logic of the code to humans and preventing the execution from the Lua software.
Syntax:
- The Lua single-line comment syntax is below.
-- The Lua comment writes here…
Description:
- The double hyphen (–) symbol is helpful for single-line comments in the text editor.
- This hyphen works until the last character or punctuation of the line.
The Lua multiple-line comment syntax is below.
--[[ write not required code or multiple line information here… ]]
Description:
- The double hyphen with a double square bracket symbol is used for multiple-line comments in the text editor.
- The comment text or multiple lines write inside of the bracket.
- The temporary code or annotations can write inside of the Lua multiple-line comment.
The combination of Lua single-line and multiple-line comment syntax is below.
--[[
-- The local variable print here…
local variable = 44
print (variable)
]]
Description:
- The single-line comment syntax is used inside of the multiple-line comment syntax.
- The single-line comment is a short text of the multiple-line comment, annotations, and temporary code.
How does comment work in Lua?
Step 1: The Lua text editor, Lua compiler, and Lua interpreter install on your computer as per the operating system and software version.
Or
If you do not have software, then you can use Lua Online IDEs for coding and start Lua programming.
Step 2: The Lua file creates with the .lua extension and writes a source code.
File name: Luacomment.lua
Step 3: If the user requires single-line comments, then Write Lua single-line comments syntax.
-- The Lua comment writes here…
If the user requires multiple line comments, then Write Lua multiple lines comments syntax.
--[[ write not required code or multiple line information here… ]]
Write the demo example of the Lua comment.
Filename:luacomment.Lua
--show the local variable of Lua language.
print ("This is Lua comment demo...")
--[[local variable = 903
print (variable)
]]
Examples
Different examples are mentioned below:-
Example #1 – Single-line comment
Code:
-- function prints a minimum number of the given two numbers.
function minimum(fnumber, snumber)
-- use if loop for condition statement.
if (fnumber < snumber) then
--return the number as per condition statement.
return fnumber;
else
return snumber;
end
--end the function method...
end
--print the minimum number here...
print ("the minimum number is", minimum(13, 17))
Description:
- The double hyphen symbol is used in many places to describe the code.
- If a single line or small text wants to display in the coding, then use single-line syntax.
- The above example displays five Lua comments to create readable code.
- This double hyphen with statement lines does not execute in the Lua compiler and interpreter.
Output:
Example #2 – Multiple-line comment
Code:
--[[ function prints a minimum number of the given two numbers. ]]
function minimum(fnumber, snumber)
--[[if (fnumber < snumber) then
return fnumber;
else
return snumber;
end
]]
if (fnumber < snumber) then
final = fnumber;
else
final = snumber;
end
return final
end
--[[print the minimum number here...
print ("the minimum number is", minimum(13, 17))
]]
print ("the minimum number is", minimum(49, 17))
Description:
- The double hyphen with a double square bracket symbol is used in many places to describe the code or avoid code execution.
- If the description or replacement of the code wants to display in the main source code, then use multiple-line syntax.
- The above example displays three Lua comments to create readable and understandable code.
- The first “if” loop does not execute, but the second “if” loop executes in the source code.
- The first print statement does not work in the source code because of the Lua comment.
- This multiple-line comment style has no effect on the source code in the Lua compiler or interpreter.
Output:
Example #3 – Combination of single and multiple lines
Code:
--[[ function prints a minimum number of the given two numbers. ]]
function minimum(fnumber, snumber)
-- use if loop for condition statement.
if (fnumber < snumber) then
--return the number as per condition statement.
return fnumber;
else
return snumber;
end
--[[
-- use if loop for condition statement.
if (fnumber < snumber) then
--return the number as per condition statement.
final = fnumber;
else
final = snumber;
end
return final]]
end
--[[
print the minimum number here...
print ("the minimum number is", minimum(13, 17))
]]
print ("the minimum number is", minimum(49, 39))
Description:
- The single-line Lua comment is used inside the second “if” loop source code.
- The second loop code is the replacement code of the first “if” loop source code.
- The information of the code is written inside of Lua comment source code with a double hyphen.
- You can see the example and their output to know how Lua’s comment work.
Output:
Example #4
Code:
--show the local variable of the Lua language.
print ("This is Lua comment demo...")
--[[local variable = 903
print (variable)
]]
Output:
Conclusion
It helps users to understand the complicated source code and workflow of the algorithm. It creates code that is readable, attractive, and simple without any execution. It creates attractive, elegant source code for developers.
Recommended Articles
We hope that this EDUCBA information on “Lua Comment” was beneficial to you. You can view EDUCBA’s recommended articles for more information.