Commenting Your Program


When you create a program in any programming language it is good to comment it. A comment is just a bit of information left by the programmer in the between the code. The comments are ignored by the computer and are only for people reading the code to see. They have four main functions. Even when you first start out writing computer programs it is a good idea to get into the habit of commenting your program - you'll be glad you did.

To comment a program all you need to do is start a line with a single quote. Have a look at the following code.


Comments that are at the start of your program like this are called Header Comments. Header Comments give information about the entire program itself. You will notice it has five different parts to it: When you have larger programs you can have comments in your code expaining what different sections do. These are called In-code Comments.

You should use Header Comments for every program that you write. In-Code comments should be used for programs that are more than a few lines long.

In-Code comments can either be on a line by itself or or at the end of a code line. As a general rule comments should only go at the end of a line of code if they are talking about that specific line. Comments usually go on a line by themselves if they are referring to a block (more than one line) of code or if they are long enough to warrant being on a line by themselves. Have a look at the following two pieces of code.



Since the computer ignores the comments it sees the exact same thing in both programs. Both of these programs are identical other than where the comment is positioned. Therefore it is up to the programmer's discretion where it goes.