Page 29 - Modular_V2.0_C++_Flikpbook
P. 29
COMMENTS
Comments are used to explain parts of the code. They can also be used to hide code temporarily.
Comments help us understand how a program works, making it more readable and maintainable.
In C++, any statement starting with // (two forward slashes) is considered a comment. Comments
are ignored by the compiler and are not executed as part of the program. While they do not
affect the functionality of the code, they improve readability and clarity. C++ supports two types
of comments: Single-line comment and Multiple-line comment.
Single-line Comment
In case a user wants to add a single-line comment, then the comment must start with the symbol
//. For example:
void main()
{
cout<<"Hello from C++."; //Printing a message.
}
When you run the above code, you will get the following output:
Hello from C++.
The text after // is ignored by the compiler and will not be displayed in the output.
Multiple-line Comment
In C++, you can add multi-line comments using /* and */. These comments can span multiple
lines and are ignored by the compiler. For example:
void main()
{
/* int age;
cout<< "Hello from C++.";
// Printing a message. */
}
When you run this above code, nothing will be printed because all lines inside /* */ are commented
out.
Recap
The cout object is used to display output on a computer screen (monitor).
The cin object is used to take input from the user through the keyboard.
Escape sequence characters are used to represent certain special characters that cannot be
directly printed on the output screen.
Comments are used to explain parts of the code.
27
Input and Output in C++

