Comments are parts of the source code disregarded by the compiler. They simply do nothing. Their purpose is only to allow the programmer to insert notes or descriptions embedded within the source code.
A comments may start anywhere in the line, and whatever follws till the end of the line is ignored.
C++ supports two ways to insert comments:
Line Comment
The first of them, known as line comment, discards everything from
where the pair of slash signs
(//)
is found up to the end of that same line. and to do multiple comment
you need to start will that double slash
(//)
at the beginning of each text.
The Example of single line Comment is:
// this is a comment
The Example of Multiple line Comments are:
// this is an example
// of multiple line
// comment in C++
Multiple Line Comment
The second one, known as block comment, discards everything between
the
(/*)
characters and the first appearance of the
(*/)
characters, with the possibility of including more than one line.
The Example of Multiple line Comment are:
/* this is an example
of multiple line
*/ comment in C++
We are going to add comments to our second program:
/* my second program in C++
with more comments */
#include <iostream>
using namespace std;
int main(){
cout<<"Hello CodingKoleji!"; // prints Hello World!
cout<<"I am a C++ Programmer"; // prints I'm a C++ Programmer
return 0;
}
We can use both styles in our code, but it is recommended to use the line comment style for short comments and the block comment style for longer comments.
If you include comments within the source code of your programs
without using the comment characters combinations
(//),
(/*)or
(*/), the compiler will take them as if they were C++ expressions, most
likely causing one or several error messages when you compile it.
(//)
style comment within the text of a program line. For example, the
double slash comment cannot be use in the manner as shown here
(for(i=0; j <n; /* loops n times */ i++))
Now that you have learned how to add comment on your C++ programming. Lets learn how to start creating Variables in C++ in the next section.