When you write a small and easy program, anyone can understand how it works just by reading it line-by-line. However, long programs are difficult to understand, sometimes even by the same person who wrote them. Comments are extra information that can be included in a program to make it easier to read and understand. You can add explanations and other pieces of information, including:

  • who wrote the program
  • when the program was created or last modified
  • what the program does
  • how the program works

However, you should not over-comment. There is no need to explain every line of your program. Add comments only when a specific portion of your program is hard to follow.

PHP

In PHP, you can add comments using one of the following methods:

double slashes //……

the sharp symbol #……

slash–asterisk, asterisk–slash delimiters /* …… */

The following program demonstrates how to use all types of commenting. Usually double slashes ( // ) and the sharp symbol ( # ) are used for commenting one single line, whereas the slash–asterisk, asterisk–slash delimiters /* …… */  are used for commenting multiple lines at once.

<?php
  /*
    Created By Bouras Aristides
    Date created: 12/25/2003
    Date modified: 04/03/2008
    Description: This script displays some messages on the screen
   */
  
  echo "Hello Zeus!";  //display a message on the screen
  
  //display a second message on the screen
  echo "Hello Hera!";

  /* display a third message on screen */  echo "Γεια σας";
  
  #This is a comment        echo "The End";
?>

Notice: If you add comments using the delimiters /* …… */  in front of a statement, the statement is still executed. In the preceding example, the Greek message “Γεια σας,” even though it is written next to some comments, is still executed. It is advisable, however, not to follow this writing style because it can make your code difficult to read.

Java

In Java, you can add comments using one of the following methods:

double slashes //……

slash–asterisk, asterisk–slash delimiters /* …… */

The following program demonstrates how to use both types of commenting. Usually double slashes ( // ) are used for commenting one single line, whereas the slash–asterisk, asterisk–slash delimiters /* …… */  are used for commenting multiple lines at once.

/*
  Created By Bouras Aristides
  Date created: 12/25/2003
  Date modified: 04/03/2008
  Description: This program displays some messages on the screen
*/

public static void main(String[] args) throws java.io.IOException {
  
  System.out.println("Hello Zeus!");  //display a message on the screen
  
  //display a second message on the screen
  System.out.println("Hello Hera!");

  /* display a third message on screen */  System.out.println("Γεια σας");
  
  //This is a comment        System.out.println("The End");
}

Notice: If you add comments using the delimiters /* …… */  in front of a statement, the statement is still executed. In the preceding example, the Greek message “Γεια σας,” even though it is written next to some comments, is still executed. It is advisable, however, not to follow this writing style because it can make your code difficult to read.

C++

In C++, you can add comments using one of the following methods:

double slashes //……

slash–asterisk, asterisk–slash delimiters /* …… */

The following program demonstrates how to use both types of commenting. Usually double slashes ( // ) are used for commenting one single line, whereas the slash–asterisk, asterisk–slash delimiters /* …… */  are used for commenting multiple lines at once.

#include <iostream>
using namespace std;
/*
  Created By Bouras Aristides
  Date created: 12/25/2003
  Date modified: 04/03/2008
  Description: This program displays some messages on the screen
*/

int main() {
  
  cout << "Hello Zeus!" << endl;  //display a message on the screen
  
  //display a second message on the screen
  cout << "Hello Hera!" << endl;

  /* display a third message on screen */  cout << "Γεια σας" << endl;
  
  //This is a comment        cout << "The End";
  return 0;
}

Notice: If you add comments using the delimiters /* …… */  in front of a statement, the statement is still executed. In the preceding example, the Greek message “Γεια σας,” even though it is written next to some comments, is still executed. It is advisable, however, not to follow this writing style because it can make your code difficult to read.

C#

In C#, you can add comments using one of the following methods:

double slashes //……

slash–asterisk, asterisk–slash delimiters /* …… */

The following program demonstrates how to use both types of commenting. Usually double slashes ( // ) are used for commenting one single line, whereas the slash–asterisk, asterisk–slash delimiters /* …… */  are used for commenting multiple lines at once.

/*
  Created By Bouras Aristides
  Date created: 12/25/2003
  Date modified: 04/03/2008
  Description: This program displays some messages on the screen
*/

static void Main() {
  
  Console.WriteLine("Hello Zeus!");  //display a message on the screen
  
  //display a second message on the screen
  Console.WriteLine("Hello Hera!");

  /* display a third message on screen */  Console.WriteLine("Γεια σας");
  
  //This is a comment        Console.Write("The End");

  Console.ReadKey();
}

Notice: If you add comments using the delimiters /* …… */  in front of a statement, the statement is still executed. In the preceding example, the Greek message “Γεια σας,” even though it is written next to some comments, is still executed. It is advisable, however, not to follow this writing style because it can make your code difficult to read.

Visual Basic

In Visual Basic, you can add comments using the apostrophe character ( ‘ ) as shown here.

'Created By Bouras Aristides
'Date created: 12/25/2003
'Date modified: 04/03/2008
'Description: This program displays some messages on the screen

Sub Main()
  Console.WriteLine("Hello Zeus!")  'display a message on the screen

  'display a second message on the screen
  Console.WriteLine("Hello Hera!")

  'This is a comment        Console.WriteLine("The End")

  Console.ReadKey()
End Sub

Python

In Python, you can add comments using the hash character ( # ) as shown here.

#Created By Bouras Aristides
#Date created: 12/25/2003
#Date modified: 04/03/2008
#Description: This program displays some messages on the screen

print("Hello Zeus!")  #display a message on the screen

#display a second message on the screen
print("Hello Hera!")

#This is a comment        print("The End")

As you can see in the preceding program, you can add comments above a statement or at the end of it, but not in front of it. Look at the last statement, which is supposed to display the message “The End.” This statement is never executed because it is considered part of the comment.

This article is taken from a book of mine. If you enjoyed it, please visit my author's page inand check out all of my books!