bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Programming/Programming Concepts
Programming•Programming Concepts

Comments in Programming

In programming, comments are text notes in your code that are ignored by the computer.

They can help you and others understand what the code does.

What is a Comment?

A comment is a section in your code that is not executed.

Comments are used to explain, clarify, or comment code for yourself or others who may read it later.

  • Comments make code easier to read and maintain
  • They help document the purpose or logic of code sections
  • They can be used to temporarily disable code for debugging

Why Use Comments?

Good comments make code understandable and maintainable.

Comments can

  • Explain complex logic
  • Describe the purpose of functions, classes, or files
  • Mark sections that need improvement
  • Make collaboration with others easier
  • Debug code by temporarily disabling lines or blocks

Types of Comments

Most programming languages support several types of comments:

  1. Single-line Comments Used for short explanations or notes. Either as a whole line: //this line is a comment print("Hello"); or at the end of a line: print("Hello"); //this is a comment

Single-line Comments

Used for short explanations or notes.

Either as a whole line

//this line is a comment
print("Hello");

or at the end of a line:

print("Hello"); //this is a comment
  1. Multi-line Comments Used for longer explanations. Syntax varies by language. /*This is a comment that spans over multiple lines*/ print("Hello");

Multi-line Comments

Used for longer explanations. Syntax varies by language.

/*This is a comment
that spans over
multiple lines*/
print("Hello");
  1. Inline Comments Comments can also appear inside a line of code. In languages that support multi-line comments, you can place a comment in the middle of a statement. This is sometimes used to quickly change a value or logic for testing. firstname = /*"John"*/"Jane"; print("Hello " + firstname); Note: This technique does not work in Python, as Python does not support block comments inside statements.

Inline Comments

Comments can also appear inside a line of code. In languages that support multi-line comments, you can place a comment in the middle of a statement.

This is sometimes used to quickly change a value or logic for testing.

firstname = /*"John"*/"Jane";
print("Hello " + firstname);

Note

This technique does not work in Python, as Python does not support block comments inside statements.

Comments in Different Languages

LanguageSingle-lineMulti-line
Python# comment""" multi-line comments """
JavaScript// comment/* multi-line comments */
Java// comment/* multi-line comments */
C// comment/* multi-line comments */
C++// comment/* multi-line comments */
HTML<!-- comment --><!-- multi-line comments -->
SQL-- comment/* multi-line comments */
Bash# comment: << 'COMMENT' multi-line comments COMMENT

Inline Comments

Inline comments are comments that appear on the same line as code, or even inside a line of code. They are useful for explaining a specific part of a statement, or quickly changing code for testing and debugging.

  • In-line (mid-line) comments: Placed inside a statement.
  • End-of-line inline comments: Placed after a statement to explain its purpose.

Using Comments for Debugging

Comments are not just for documentation-they can also be a powerful debugging tool. When troubleshooting code, you can temporarily "comment out" lines or blocks to isolate problems or prevent certain code from running.

  • Comment out code: Disable parts of your code without deleting them, making it easy to restore later.
  • Test changes safely: Try new logic while keeping the original code as comments for reference.
  • Isolate bugs: By commenting out sections, you can narrow down where errors are happening.

Best Practices for Comments

  • Write clear and concise comments
  • Keep comments up-to-date with your code
  • Don't state the obvious (avoid "increment x by 1" for x += 1 )
  • Use comments to explain why , not just what
  • Remove outdated or misleading comments

Summary

Comments are a vital tool for making your code readable, maintainable, and collaborative. Use them wisely!

Previous

Bitwise Operators in Programming

Next

Input and Output in Programming

This chapter

Overview
25

Lessons

105m

Read time

1. Understanding The Concepts of Programming2. What is Programming ?3. Variables in Programming4. Constants in Programming5. If Statements in Programming6. Arrays in Programming7. Loops in Programming8. Functions in Programming9. Recursion in Programming10. Scope in Programming11. Strings in Programming12. Data Types in Programming13. Type Casting in Programming14. Operators in Programming15. Arithmetic Operators in Programming16. Assignment Operators in Programming17. Comparison Operators in Programming18. Logical Operators in Programming19. Bitwise Operators in Programming20. Comments in Programming21. Input and Output in Programming22. Bits and Bytes in Programming23. Binary Numbers in Programming24. Hexadecimal Numbers in Programming25. Boolean Algebra

On this page

What is a Comment?Why Use Comments?Types of CommentsSingle-line CommentsMulti-line CommentsInline CommentsComments in Different LanguagesInline CommentsUsing Comments for DebuggingBest Practices for CommentsSummary