1.3 — Comments

📘 Chapter 1⏱ about 1 min read✍️ Beginner friendly
Read one box at a time. No rush.

Notes for people#

A comment is a note in your code. The computer ignores it completely.

C++
// This is a one-line comment.std::cout << "Hi\n"; // a comment can go at the end of a line too

Everything after // on that line is ignored.

Many-line comments#

Use /* to start and */ to end.

C++
/* This comment   goes over   many lines. */

What makes a good comment?#

Good comments explain why, not just what.

C++ — wrong ✗
x = x + 1; // add one to x
C++ — right ✓
lives = lives + 1; // bonus life for finishing the level

The code already says what happens. The comment tells us why.

Turning code off#

You can put // in front of a line to switch it off for a moment. This is called commenting out.

C++
std::cout << "This shows\n";// std::cout << "This does not\n";
Tip

Commenting out is great when looking for a bug. Switch a line off, run again, and see what changes.

Finished reading? Tick it off. Your progress is saved in this browser.

Tip: press ← / → to move between lessons, / to search.