1.3 — Comments
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 tooEverything 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 xC++ — right ✓
lives = lives + 1; // bonus life for finishing the levelThe 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.
Tip: press ← / → to move between lessons, / to search.