Comments in C++

Comments in programming language aren't executable statements - the compiler ignores them completely and they have no effect on what the program does, how it runs, or what output it produces.

Instead, they are notes from the programmer to themselves (or to other programmers who are reading the code).

Generally they are used to explain the purpose or appropriate use of particular segments of code.

There are two types of comments in C++: single and multi-line.

Single line comments begin with // and continue to the end of the line.

Multi-line comments begin with /* and continue (possibly for many lines), ending with */

The program below has examples of both types of comment

/*  This program lets the user enter numbers to sum together,
    and after each number asks if they wish to quit or continue */

#include <cstdio>

int main()
{
   char cmd;  // this variable stores user commands
   float sum;  // this variable stores a running total

   // initialize the running total to zero
   sum = 0;

   /* Keep adding user-entered numbers together until
      the user decides to quit */
   do {
      // get the user to enter a number
      printf("Enter a number to add to the running total\n");
      float userValue;
      scanf("%f", &userValue);

      // add it to the running total
      sum = sum + userValue;

      // determine if they wish to quit or get more numbers
      printf("Enter Q to quit, anything else to continue\n");
      scanf("%c", &cmd);

   } while (cmd != 'Q');

   // display the final total
   printf("The sum of the numbers was %f\n", sum);
}