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. The Lua comment is the operator used to ignore unwanted programming and information of the primary source code from the Lua IDE. The Lua comment helps to understand the main source code and provide explanation or information about code without execution. The Lua comment is an operator using to provide readable information about code in the text editor but does not execute from the Lua compiler and Lua interpreter. The Lua comment is small text or information used for understands critical code or logic of the code to human and prevent 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 useful 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 double square bracket symbol is using 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 using 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 in 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 required single line comments then Write Lua single line comments syntax.
-- The Lua comment writes here…
If the user required 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
Let us discuss examples of Lua Comment.
Example #1: Lua single line comment example and output
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 Lua interpreter.
Output:
Example #2: Lua multiple line comment example and output
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 double square bracket symbol is used in many places to describe the code or avoid code execution.
- If 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 lines comment syntax does not affect to source code in the Lua compiler and Lua interpreter.
Output:
Example #3: the combination of the single and multiple lines Lua comment example and output
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 used inside of 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 writing 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: the basic Lua comment example and output
Code:
--show the local variable of the Lua language.
print ("This is Lua comment demo...")
--[[local variable = 903
print (variable)
]]
Output:
Conclusion
The Lua comment helps users to understand the complicated source code and workflow of the algorithm. The Lua comment is creating code readable, attractive, and simple without any execution. The Lua comment creates attractive, elegant source code for developers.
Recommended Articles
This is a guide to Lua Comment. Here we discuss the definition, How does comment work in Lua, and examples with code implementation. You may also have a look at the following articles to learn more –
41 Online Courses | 13 Hands-on Projects | 322+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses