How to comment in different programming languages

What are comments in programming languages?

Comments in programming languages are used to document the programs. Comments are non-executable and are used to explain the code written in the program.

Different programming languages have different syntaxes for writing the comments in the program. Let’s have a look at a few of them.

Comments in C++, Javascript, JAVA, and PHP

There are two ways to comment in C+, Javascript, JAVA, and PHP:

  • Single-line comments //
  • Multi-line comments /*

In single-line commenting, the text starting with // to the end of the line is ignored by the compiler and not executed.
In multiline commenting, the code between /* and */ is considered as comments and not executed.

#include <iostream>
using namespace std;
int main() {
// This is the comment and is ignored by the compiler.
cout << "This line is executed by the compiler." << endl;
/*
this
is
multiline
comment
*/
cout << "After the comments this code is executed.";
return 0;
}
Demonstration of single line comments in different programming languages using // and /*

Comments in Python, Ruby, and R

There are two ways to comment in Python, Ruby, and R:

  • Single-line comments #
  • Multi-line comments

Single-line comments

print "Hello World"
# this is single line comments
Demonstration of single-line comments in different programming languages using #

Multi-line comments

Note: R does not support multi-line comments.

Multi-line comments for Python and Ruby are given below:

print "Hello World"
"""
this is the
way to write
multiline comment in python
"""
Demonstration of multi-line comments in Python and Ruby

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved