Comments

What are comments?

Comments are text added inside the code in order to explain what it does. Comments are ignored by the compiler, they are there only for the programmers reading the code.

Comments on one line

We can write a command standing on one line by starting the comment by two slashes characters //. All the text, on that line, following the slashes //, will be considered a comment.

// I am a comment int var = 4; // That line defines the variable var with the value 4.

Multi-line comments

It is possible to write a comment on many lines by starting it with a slash character followed by an asterisk /* and ending it with an asterisk followed by a slash */.

1
2
3
4
5
6
7
float var = 5.2; /* Hello, I am a comment written on multiple lines. */ float var2 = var + 3.1; /* Assigns the result of the summation of var and 3.1 to var2. */

When to comment

It is recommended to comment the code only when it is hard to understand, when what it does is not obvious or to warn people reading it about something. Comments are there to fight ambiguity, not to make the code heavier.

Note that in this course, way more comments than normally needed are included in order to make sure everybody understands the examples.

Why writting comments?

Comments are very useful when we work with other programmers on the same project or to help us understand lines of code we wrote weeks, months or even years ago.