TL ; DR:
Comments are lines of code that C++ will intentionally ignore (not execute). They don't do anything.
You add a single line comment in C++ by typing
//followed by any text:// This is a comment! cout << "This is not!" << endl;
Full lesson:
Comments are lines of code that C++ will intentionally ignore. They don't do anything.
They're just used to create notes for yourself and others about what the code does.
There are two types of code comments in C++:
1. Single line comments:
We create single line comments by typing // and C++ will ignore (not execute) any text between // and the end of the line.
Single line comments automatically end at the next line of code:
// Next line will greet the user:
cout << "Hello user!";
Output of this code:
Hello user!
We can also use single line comments at the end of a line to explain the code:
cout << "Hello user!" << endl; // This line greets the user
cout << "This is not a comment!";
Output of this code:
Hello user!
This is not a comment!
2. Multi-line comments
We can also comment multiple lines of code using multi-line comments.
We type /* to begin the comment and type */ to end the comment:
/* This is a
multi-line comment
cout << "This will not run";
Next line will greet the user:
*/
cout << "Hello user!";
Output of this code:
Hello user!
Assignment
Follow the Coding Tutorial and let's write some comments!
Hint
Look at the examples above if you get stuck.
Free plan: 30 tutor messages. PRO is unlimited.
