Lesson 8 of 60 – Comments in C++
13%

Comments in C++

Comments are explanatory notes written inside a C++ program. They are mainly used to make source code easier to understand and maintain. Comments are ignored by the compiler when it processes the program.

Note: C++ supports two common types of comments: single-line comments using // and multi-line comments using /* */.

1. What are Comments?

A comment is text written in source code to explain what the code does. It is not executed as part of the program.

// This is a comment

The compiler ignores the comment as program instructions.

2. Why Use Comments?

Comments can make programs easier to read and understand. They are especially useful when a program contains complex logic.

  • Explain the purpose of code
  • Describe important logic
  • Make code easier to maintain
  • Help beginners understand programs
  • Temporarily disable code during testing

3. Types of Comments in C++

C++ provides two commonly used comment styles:

  • Single-line comments
  • Multi-line comments
// Single-line comment

/*
   Multi-line comment
*/

4. Single-Line Comments

A single-line comment begins with two forward slashes: //

// This is a single-line comment

Everything after // on that line is treated as a comment.

5. Simple Single-Line Example

#include <iostream>

int main() {

    // Display a message
    std::cout << "Hello C++";

    return 0;
}

The comment explains the purpose of the output statement.

6. Comment After a Statement

A single-line comment can also be placed after a statement.

int age = 20; // Store the age

The compiler processes the statement and ignores the comment.

7. Commenting a Variable

int age = 20; // Student's age
double fee = 5000; // Course fee

Comments can explain what different variables represent.

8. Multi-Line Comments

A multi-line comment begins with /* and ends with */. It can span multiple lines.

/*
   This is a
   multi-line comment
*/

9. Multi-Line Comment Example

#include <iostream>

/*
   This program displays
   a welcome message.
*/

int main() {

    std::cout << "Welcome to C++";

    return 0;
}

10. Commenting Multiple Lines

Multi-line comments are useful when you want to describe a larger section of code.

/*
   Calculate the total
   and display the result.
*/

int total = price + tax;

11. Comments Do Not Execute

Comments are not executed by the program.

// std::cout << "Hello";

The output statement above is commented out, so it does not execute.

12. Temporarily Disable Code

Comments can be used to temporarily prevent a line of code from executing.

#include <iostream>

int main() {

    std::cout << "Hello";

    // std::cout << "This line is disabled";

    return 0;
}

The commented statement is ignored.

13. Using Comments for Explanation

int marks = 80;

// Check whether the student passed
if (marks >= 40) {

    std::cout << "Pass";
}

The comment explains the purpose of the condition.

14. Comments Before Code

A comment can be placed before the code it describes.

// Calculate the sum
int sum = a + b;

This style can make the purpose of the following statement clear.

15. Comments After Code

A short comment can be placed at the end of a statement.

int result = a + b; // Add two numbers

This is useful when the explanation is short.

16. Comments in Functions

Comments can explain the purpose of a function.

// Calculate the sum of two numbers
int add(int a, int b) {

    return a + b;
}

17. Comments in Loops

for (int i = 1; i <= 5; i++) {

    // Display the current number
    std::cout << i << std::endl;
}

The comment describes what happens inside the loop.

18. Comments in if Statements

if (age >= 18) {

    // Person is an adult
    std::cout << "Adult";
}

Comments can explain the purpose of a condition.

19. Multiple Single-Line Comments

You can write multiple single-line comments one after another.

// Program name: Student
// Store student age
// Display student age

int age = 20;

std::cout << age;

20. Combining Comment Styles

Single-line and multi-line comments can be used in the same program.

// Program starts here

/*
   Store and display
   student information.
*/

int age = 20;

std::cout << age;

21. Comments and Readability

Comments should help readers understand the code. Good comments explain the purpose or reasoning behind code when that information is not obvious from the code itself.

// Calculate the final price after applying the discount
double finalPrice = price - discount;

22. Avoid Unnecessary Comments

Not every line of code needs a comment. A comment that simply repeats what the code already clearly says may not add useful information.

int age = 20; // Set age to 20

For simple and obvious code, meaningful variable names can often make the purpose clear.

23. Comments for Program Documentation

Comments can provide useful documentation about a program, function, algorithm, or important implementation decision.

/*
   Calculate the average of
   three student marks.
*/

double average = (a + b + c) / 3.0;

24. Comment Syntax with Strings

Comment markers inside a string literal are treated as characters in the string rather than as comment syntax.

std::cout << "This is not a // comment";

Here, // is part of the displayed string.

25. Multi-Line Comment Boundaries

A multi-line comment starts with /* and continues until the first matching */.

/*
   This entire section
   is a comment.
*/

std::cout << "Hello";

Anything between the delimiters is treated as part of the comment.

26. Common Comment Mistakes

Beginners may make mistakes such as:

  • Forgetting the closing */
  • Using incorrect comment symbols
  • Writing overly long comments for simple code
  • Writing comments that no longer match the code
  • Commenting code accidentally during editing

27. Good Comment Example

#include <iostream>

int main() {

    int marks = 75;

    // Check whether the student has passed
    if (marks >= 40) {

        std::cout << "Student Passed";

    }

    return 0;
}

The comment explains the purpose of the condition.

28. Comments in a Larger Program

In larger programs, comments can help organize sections of code.

// Student Information
int age = 20;
double marks = 85.5;

// Display Information
std::cout << age;
std::cout << marks;

Useful section comments can make longer files easier to navigate.

29. Single-Line vs Multi-Line Comments

Type Syntax Use
Single-Line // comment Short explanations or one-line comments
Multi-Line /* comment */ Comments covering multiple lines

30. Complete Comments Example

#include <iostream>

/*
   This program demonstrates
   comments in C++.
*/

int main() {

    // Store the student's marks
    int marks = 80;

    // Check the result
    if (marks >= 40) {

        std::cout << "Pass";

    } else {

        std::cout << "Fail";
    }

    return 0;
}

This example uses both single-line and multi-line comments to explain different parts of the program.

📌 Key Points

  • Comments are used to explain C++ source code.
  • Comments are not executed as program instructions.
  • // is used for single-line comments.
  • /* */ is used for multi-line comments.
  • Comments can improve code readability and maintainability.
  • Comments can temporarily disable code during testing.
  • Comments can be used inside functions, loops, and conditional statements.
  • Useful comments explain purpose or logic rather than simply repeating obvious code.
  • Multi-line comments must have a closing */.
  • Good comments should remain consistent with the code.

🧠 Quick Quiz

Question: Which symbol is used to start a single-line comment in C++?