Comments
Programs are read not only by computers, but also by people: by other programmers, and by you yourself a few weeks later, when you no longer remember why you wrote something the way you did. That's why programming languages have comments: notes in the code that the computer ignores.
In Lua, a comment starts with two dashes, --. Lua ignores everything after them, up to the end of the line. A comment can be on a line of its own, or at the end of a line with code:
-- The price of a bag of carrots
print(6 * 5) -- one carrot sells for 5 coinsThis prints only:
30A longer note goes between --[[ and ]]. Such a comment can span several lines:
--[[
Sunflower Farm
Prints Emma's plan for Monday.
]]
print("Monday: plant the potatoes")This prints:
Monday: plant the potatoesA good comment says what you can't see in the code at first sight: what a part of the program is for, or why it's written the way it is. A comment like -- prints Hello above print("Hello") doesn't help anyone.
Comments are also useful for switching code off for a while without deleting it. Programmers call this commenting out: put -- at the start of a line, and the line no longer runs. When you want it back, just delete the --:
print("This line runs.")
-- print("This one doesn't.")This prints:
This line runs.To comment out several lines at once, put them between --[[ and ]]. Just don't forget the ]]. Without it, everything up to the end of the program becomes a comment, and Lua reports an error that says unfinished long comment (starting at line 1) near <eof>: the long comment that starts on line 1 never ends, and Lua got to <eof>, the end of file, while it was still looking for the ]].
Instructions
At the top of the program, add a comment that says what the program does, for example -- Emma's market list.
It's summer, so Emma doesn't need a snow shovel. Comment out the line that prints snow shovel, so that the program prints only:
Taking to the market:
basket
carrots
pumpkinsAt the end of the program, add a longer note between --[[ and ]], for example that the market opens at 8 in the morning and that Hugo the trader will be there too. Lua ignores the note, so the output stays the same.
Start programming for free
5/7