Topic :Comments in C++: Single Line Comments | Multiline Comments:

Comments in C++:

Comments are portions of the code ignored by the compiler which allow the user to make simple Notes in the relevant areas of the source code. Comments come either in block from or as single lines.



Single line Comments:

Single line Comment start with //(double slash) symbol which tell the compiler to ignore everything to the end of the line.



Example:
#include<iostream>
using namespace std;
main()
{
    //This is an single line comment and 
    //will be ignored by compiler.
}


Typically, the single line comment is used to make a Quick Comment about a Single line of Code.



Multi-line Comments:

Multi-line Comment start with /* and end with */ which tell the compiler to ignore everything between these two symbols.



Example:
#include<iostream>
using namespace std;
main()
{
   /*This is a Multi-line comment and 
      will be ignored by the compiler.*/
}


Since everything between the symbols is ignored, you will sometimes see Programmers use mylt-line comment to beautify their code.



Proper Use of Comments:

Comment should be used for libraries, Function, or class, and can describe what this program can do. The comment should be logical, which can give the reader a good idea of what the Program is trying to Accomplish without having to look at the actual code. The user(Possibly someone else, or you if you're trying to reuse code you're already previously written) can tell at a glance whether the code is relevant to what he or she is trying to accomplish. This is Particularly important when working as part of a team, where not everybody will be familiar with all of the code.
At the statement level, Comments should be used to describe why the code is doing something. A bad statement comment explains what the code is doing. if you ever write code that is so complex that needs a comment to explain what a statement is doing, you Probably need to rewrite your statement, not comment it.



Bad way of Comments:


#include<iostream>
using namespace std;
main()
{
    int i;
    for(i=0;i<10;i++) //this is for loop.
    {
        cout<<"Welcome to infobrother\n";
    }
    return 0;
}

the comment (this is for loop) infront of for loop doesn't make any sense. because we already know that its an for loop. so don't need to put comment here. so its an bad Programming practice to write comments like this way.



Good way of Comments:


#include<iostream>
using namespace std;
main()
{
    int i;
    for(i=0;i<10;i++) //this loop will repeat the statement 10 times.
    {
        cout<<"Welcome to infobrother\n";
    }
    return 0;
}

But here, this comment can make sense. because it shows the function of that loop. so its a good Programming practice to write comments like this way.



Commenting is the art of describing what your Program is going to do. so it's the good Programming Practice to put comments in your Program. so anyone can understand your code. and after a long time if you need to reuse you written code, then your comments will help you to understand the code. if you didn't put any code then it will be very difficult to understand the code and reuse it.





I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement