Comments are sections of a computer program that the computer ignores. The computer skips over comments. It does not read them.
So what purpose do they serve? They are the places where you put the explanations for your program. They are meant to be read only by humans, not by computers.
If you are working on a part of your program that calculates the slope of a line, you might want to start that section with a comment that states 'slope calculation' or 'calculating slope'. Such a comment would look like this:
// Slope calculation
This line of code starts with two forward slashes, one after the other. The slashes are what makes this line a comment. The computer sees the slashes and skips over the line.
Comments are meant to be read as messages about the program. They help the program's author remember what the program is doing, and they help other readers understand it.
You will notice that comments are covered first in the tutorial section. This is because they will hopefully be very useful to you as you study the example programs that come with each of the following tutorials. The details of their example programs are often explained with comments.
They are simply notes in the code which will help you understand the program.
In general, the computer reads your program left to right, top to bottom. If the computer encounters a double forward slash as it reads from left to right across a line of code, then the computer skips over what ever is past the slashes for the rest of that line. The computer starts paying attention again on the next line.
If the double slashes come at the start of a line, then the entire line is a comment, and the entire line is ignored by the computer.
// This entire line is a one line comment.
Note the space after the double slashes. It makes the comment easy to read and looks good, but it is not essential:
The double slashes can also create a comment half way across a line. That is, the comment can be the last section on a line after some code that the computer was reading and acting upon. We have yet to discuss any commands in this tutorial section, but let's just jump ahead: The command print prints words. Here is an example with a comment included:
print('Hello world.'); // Output a greeting.
As the computer scans the above line of code, left to right, it would encounter the print command, understand it, and print the message 'Hello world.' At that point we would be past the semi-colon. Next in line are the slashes. They start a comment. The rest of the line would be skipped.
You might have a lot to say in a comment, a few lines worth, perhaps. One way to create a comment for several lines is simply to make several one line comments:
// This is a long comment. // It is more than two lines long. // It is three lines long.
This way of making comments is different from the double slash method. This way uses two markers.
The starting comment marker is two characters long, a forward slash followed by an asterisk:
The ending comment marker is also two characters long, an asterisk followed by a forward slash:
Once the computer reads the starting marker, it skips everything after it, line after line, until it comes across the ending marker. Then it starts reading again. Here is an example:
/* This, too, is a long comment. It, too, is more than two lines long. Like the previous, it is three lines long. */
/* You can make single line comments this way. */
print('Hello world.'); /* This is fine, too. */
Comments can be billboards:
// ====================== // Big important comment! // ======================
The example program that follows actually does nothing. It is made up only of comments, and the computer ignores all of them. Click the 'Go' button on the graph panel to run the program and see nothing happen. The loop code, though, will be cycling. If you click the 'Stop' button, you will stop the loop code from repeatedly doing nothing.
Actually, the comments do make for an informative read. They give a short explanation for each code section.
Next tutorial: The graph at startup