Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++
java
python
javascript
ruby

How to comment in different programming languages

Hammad Qayyum

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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

RELATED TAGS

c++
java
python
javascript
ruby

CONTRIBUTOR

Hammad Qayyum
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring