Comments allow us to include notes and explanations within the code. These notes help make the code easier to understand for both other programmers and for ourselves when reviewing the project in the future.
Additionally, comments allow us to temporarily disable code snippets without deleting them, which is useful for testing and debugging (but only for testing, leaving it there is a mess 😊).
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 multiple lines.
If you want to learn about Comments in programming
check out the Introduction to Programming Course read more
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 characters will be considered a comment and will not be executed by the compiler.
/* This is a
multi-line
comment */