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.
In addition, comments allow temporarily disabling chunks of code without deleting them, which is useful for testing and debugging (but only for testing, leaving it 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 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 */