Comments in C++


TL ; DR:

  • Comments are lines of code that C++ will intentionally ignore (not execute). They don't do anything.


  • You add a single line comment in C++ by typing // followed by any text:

    // This is a comment!
    cout << "This is not!" << endl;
    




Full lesson:

Comments are lines of code that C++ will intentionally ignore. They don't do anything.

They're just used to create notes for yourself and others about what the code does.

There are two types of code comments in C++:


1. Single line comments:

We create single line comments by typing // and C++ will ignore (not execute) any text between // and the end of the line.

Single line comments automatically end at the next line of code:

// Next line will greet the user:
cout << "Hello user!";

Output of this code:

Hello user!

We can also use single line comments at the end of a line to explain the code:

cout << "Hello user!" << endl; // This line greets the user
cout << "This is not a comment!";

Output of this code:

Hello user!
This is not a comment!

2. Multi-line comments

We can also comment multiple lines of code using multi-line comments.

We type /* to begin the comment and type */ to end the comment:

/* This is a 
multi-line comment
cout << "This will not run";
Next line will greet the user:
*/

cout << "Hello user!";

Output of this code:

Hello user!

Assignment
Follow the Coding Tutorial and let's write some comments!


Hint
Look at the examples above if you get stuck.


Introduction

In this lesson, we will explore the concept of comments in C++. Comments are an essential part of programming as they help in making the code more readable and maintainable. They are particularly useful for documenting the code, explaining complex logic, and leaving notes for future reference. Understanding how to use comments effectively is crucial for writing clean and understandable code.

Understanding the Basics

Comments in C++ are lines of code that the compiler ignores. They do not affect the execution of the program. There are two types of comments in C++:

Let's look at some simple examples to illustrate these concepts:

// This is a single-line comment
cout << "Hello, World!" << endl; // This is another single-line comment

/* This is a multi-line comment
   It can span multiple lines
   The compiler will ignore all of this */
cout << "Hello, again!" << endl;

Main Concepts

Comments are used to explain the code and make it more readable. Here are some key points to remember:

Let's see how to apply these concepts with examples:

// Function to add two numbers
int add(int a, int b) {
    return a + b; // Return the sum of a and b
}

/* Main function
   This is where the program starts */
int main() {
    int result = add(5, 3); // Call the add function
    cout << "The result is " << result << endl; // Print the result
    return 0; // End of the program
}

Examples and Use Cases

Let's look at some examples that demonstrate the use of comments in various contexts:

// Example 1: Simple program with comments
#include <iostream>
using namespace std;

int main() {
    // Print a greeting message
    cout << "Hello, World!" << endl;
    return 0; // End of the program
}

// Example 2: Function with comments
#include <iostream>
using namespace std;

// Function to multiply two numbers
int multiply(int a, int b) {
    return a * b; // Return the product of a and b
}

int main() {
    int result = multiply(4, 5); // Call the multiply function
    cout << "The result is " << result << endl; // Print the result
    return 0; // End of the program
}

Common Pitfalls and Best Practices

Here are some common mistakes to avoid when using comments:

Best practices for writing comments:

Advanced Techniques

As you become more experienced, you may encounter advanced commenting techniques such as:

Example of a documentation comment:

/**
 * @brief Adds two integers.
 * 
 * This function takes two integers as input and returns their sum.
 * 
 * @param a First integer
 * @param b Second integer
 * @return int Sum of a and b
 */
int add(int a, int b) {
    return a + b;
}

Code Implementation

Here is a well-commented code snippet demonstrating the use of comments in a real-world scenario:

#include <iostream>
using namespace std;

/**
 * @brief Calculates the factorial of a number.
 * 
 * This function takes a non-negative integer as input and returns its factorial.
 * 
 * @param n Non-negative integer
 * @return int Factorial of n
 */
int factorial(int n) {
    // Base case: factorial of 0 is 1
    if (n == 0) {
        return 1;
    }
    // Recursive case: n * factorial of (n-1)
    return n * factorial(n - 1);
}

int main() {
    int number = 5; // Number to calculate the factorial of
    int result = factorial(number); // Call the factorial function
    cout << "Factorial of " << number << " is " << result << endl; // Print the result
    return 0; // End of the program
}

Debugging and Testing

When debugging code, comments can be very helpful. Here are some tips:

Example of using comments for debugging:

#include <iostream>
using namespace std;

int add(int a, int b) {
    return a + b;
}

int main() {
    int x = 10;
    int y = 20;
    // Debugging: Check the values of x and y
    // cout << "x: " << x << ", y: " << y << endl;
    int result = add(x, y);
    cout << "Result: " << result << endl;
    return 0;
}

To test your code, you can write test cases that verify the correctness of your functions. Here is an example:

#include <iostream>
#include <cassert>
using namespace std;

int add(int a, int b) {
    return a + b;
}

void test_add() {
    assert(add(2, 3) == 5);
    assert(add(-1, 1) == 0);
    assert(add(0, 0) == 0);
    cout << "All test cases passed!" << endl;
}

int main() {
    test_add(); // Run the test cases
    return 0;
}

Thinking and Problem-Solving Tips

When approaching problems related to comments and code documentation, consider the following strategies:

Conclusion

In this lesson, we covered the importance of comments in C++, the different types of comments, and best practices for using them. Comments are a powerful tool for making your code more readable, maintainable, and understandable. By mastering the use of comments, you can write better code and collaborate more effectively with others.

Remember to practice writing comments and explore further applications to improve your skills.

Additional Resources

Here are some additional resources to help you learn more about comments and code documentation: