Comments
A comment is a note in your code for people to read. Lua skips comments completely, so they don't change what the program does.
Lua has two kinds of comments:
-- This is a one-line comment.
print("Hello")
--[[ This comment can
go over many lines. ]]
print("Bye")
Output:
Hello
Bye
-- starts a comment that goes to the end of the line. --[[ starts a comment that goes on until ]], over as many lines as you need. Neither comment shows up in the output.
A one-line comment can also sit at the end of a line of code. Lua runs the code and skips the rest of the line:
print("Total: 90") -- 10% off for members
Output:
Total: 90
What are comments for?
To explain why. Code shows what happens, but not the reason. A note like -- 10% off for members tells the next reader something the code alone can't.
To switch code off for a while. Sometimes you want a line to stop running, but you don't want to delete it. Put -- in front of it:
print("Start")
-- print("checking the total")
print("End")
Output:
Start
End
The middle line is now a comment, so it doesn't run. This is called commenting out a line. To bring it back, remove the --. Programmers do this all the time while they look for bugs.
Don't write comments that only repeat the code. -- print Start above print("Start") tells the reader nothing new.
In the exercise, comment out the line that should no longer print.