Comments allow us to include notes and explanations within the code. These notes help make the code easier to understand for other programmers, as well as for ourselves when reviewing the project in the future.
Furthermore, comments allow temporarily disabling code fragments without deleting them, which is useful for testing and debugging (but only for testing, leaving them there is messy 😊).
In C++, there are two ways to write comments in the code:
Single-line comments: Used for brief comments that extend to the end of a line.
Multi-line comments: Used for more extensive comments that span several lines.
If you want to learn more, check out the Introduction to Programming Course
Single-Line Comments
Single-line comments in C++ are written using two forward slashes (//) at the beginning of the line.
Everything after these two slashes on the same line will be considered a comment and will not be executed by the compiler.
// This is a single-line comment
Multi-Line Comments
On the other hand, multi-line comments in C++ are written using a forward slash and an asterisk at the beginning /* and an asterisk and a forward slash */ at the end of the comment.
Everything between these two character sequences will be considered a comment and will not be executed by the compiler.
/* This is a
multi-line
comment */
