Published: 07 May 2018 › Updated: 07 May 2018
Learn to code in C++ (lesson 3) - comments
//. When C++ sees //, the compiler with automatically ignore everything after the // on that line.
You might be wondering why you would want to include any "code" that is totally ignored. Well, remember when we spoke about good programming style? It is good practice to include comments in your program explaining what the program does (or even what parts of your program do).
As an example, it is best practice to include a comment statement at the whole beginning of your program letting another programmer know what the program actually does. Like this:
//Display "Hello World" on the screen
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
return 0;
}
Going forward, make sure that you use a comment statement at the beginning of all your programs explaining the purpose of your program.
Remember, everything after the // on that line is ignored. Take this program as an example:
//Display text on different lines
#include <iostream>
using namespace std;
int main()
{
cout << "Line 1" << endl;
cout << "Line 2";
return 0;
}
The output would be:
Line 1
Line 2
Now we add // before the first cout statement:
//Display text on different lines
#include <iostream>
using namespace std;
int main()
{
//cout << "Line 1" << endl;
cout << "Line 2";
return 0;
}
Now the first cout statement is totally ignored thus the output would be:
Line 2
I hope you understand the importance of comments statements and how useful they can be. You will see that we will be using these a lot as our programs start to grow 😎
Interested in additional content, including activities and solutions?
See my blog here.
Team South Africa banner designed by @bearone
Please upvote if you liked this post, follow if you want to see more, and let me know your thoughts in the comments.
Leave Learn to code in C++ (lesson 3) - comments to:
Read more #steemiteducation posts
Best Posts From cheeto.blue
We have not curated any of cheeto.blue's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From cheeto.blue
- 1926 Chevrolet "Superior" Pick Up Truck - Franschhoek Motor Museum
- Learn to code in C++ (lesson 3) - comments
- Learn to code in C++ (lesson 2) - new line
- Learn to code in C++ (lesson 1) - Hello, World!
- Learn to code in C++ (lesson 0) - install a C++ IDE
- Back to work - hello Steemit!!
- 1914 Humbrette Model 8 - Franschhoek Motor Museum
- 1911 Ford Model T - Franschhoek Motor Museum
- 1904 Mars Carette - Franschhoek Motor Museum
- Allen family