Commenting your C++ code

Commenting your code as you write it is one of the best ways to learn a new programming language. It is more effective to comment each line of code as you write it when it's fresh in your mind.

When to comment your code

Writing your program and then going back and commenting all the lines is much less effective and is considered by many to be bad practice (mainly because many people can't be bothered so the comments aren't as thorough as they would have been otherwise).

Where to comment your code

You should place comments either above the line that you're commenting or to the right.

Never place a comment below the line of code that you're commenting!

Single line comments

You can put a comment on a single line by starting the comment with two forward-slashes like this: //

Everything after the forward slashes will be ignored by the compiler.

// This is a single line comment

Multi-line comments

You can create a comment block that spans more than one line by using /* and */ to start and end the comment.

/* This is a comment that
   can span as many lines as
   I need to explain my code. */