0% found this document useful (0 votes)
21 views3 pages

2.) Comments in CPP

The document explains the purpose and types of comments in C++, highlighting their role in improving code readability and aiding debugging. It details single-line comments, which use '//' to comment out a single line, and multi-line comments, which use '/*' and '*/' to comment out multiple lines. Examples of both types of comments are provided to illustrate their usage in C++ programming.

Uploaded by

raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

2.) Comments in CPP

The document explains the purpose and types of comments in C++, highlighting their role in improving code readability and aiding debugging. It details single-line comments, which use '//' to comment out a single line, and multi-line comments, which use '/*' and '*/' to comment out multiple lines. Examples of both types of comments are provided to illustrate their usage in C++ programming.

Uploaded by

raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

C++ Programming

Topperworld.in

Comments

• Comments in C++ are meant to explain the code as well as to make it


more readable.
• When testing alternative code, it can also be used to prevent execution.
• The purpose of the comments is to provide information about code lines.
Programmers commonly use comments to document their work.

❖ Why Comments are used in C++?

Comments in C++ are used to summarize an algorithm, identify a variable’s


purpose, or clarify a code segment that appears unclear. Comments are also
used for:
• Comments are used for easier debugging.
• It makes a program more readable and gives an overall description of
the code.
• Comments are helpful in skipping the execution of some parts of the
code.
• Every time a program or code is reused after long periods of time, the
comment recaps all the information of the code quickly.

❖ Types of Comments in C++


In C++ there are two types of comments in C++:
• Single-line comment
• Multi-line comment

1. Single Line Comment

In C++ Single line comments are represented as // double forward slash. It


applies comments to a single line only.
The compiler ignores any text after // and it will not be executed.

©Topperworld
C++ Programming

Syntax:
// Single line comment

Example:
// C++ Program to demonstrate single line comment
#include <iostream>
using namespace std;

int main()
{
// Single line comment which will be ignored by the
// compiler
cout << "Topper World!";
return 0;
}

Output:

Topper World!

2. Multi-Line Comment

A multi-line comment can occupy many lines of code, it starts with /* and
ends with */, but it cannot be nested.
Any text between /* and */ will be ignored by the compiler.

©Topperworld
C++ Programming

Syntax:
/*
Multiline Comment
.
*/

Example:

// C++ Program to demonstrate Multi line comment


#include <iostream>
using namespace std;

int main()
{
/* Multi line comment which
will be ignored by the compiler
*/
cout << "Topper World!";
return 0;
}

Output:

Topper World!

©Topperworld

You might also like