Friday, October 21, 2011

1.7. Comments

We can write comments, which the compiler ignores, but which may serve us to clarify the logic of the program. They are written between "/*" and "*/":

int sum; /* Because we keep it for later use */

It is advisable to write comments which help us clarify the mission of the parts of our programs that may be less obvious at a glance. It is frequently recommended also to start the program with a comment, to remind us what the program does, not needing to read all the source. An almost too commented example:

/* ---- C# example: sum of two preset numbers ---- */
 
public class Example02b {
    public static void Main()
    {
        int firstNumber;
        int secondNumber;
        int sum;  /* Because we keep it for later use */
 
        firstNumber = 234;
        secondNumber = 567;
        /* We calculate the sum first */        
        sum = firstNumber + secondNumber;
          /* And finally we display the result */
        System.Console.WriteLine("The sum of {0} and {1} is {2}", 
          firstNumber, secondNumber, sum);
    }
}
A comment can start on one line and end in a different one:

/* This
is a comment
more than one line long
*/


We can also use other commenting style: comments which begin with "//" and finish at the end of the current line (so they cannot occupy more than one line). They are "C++ style comments":

// This is a "C++ style" comment

No comments:

Post a Comment