Notes to Yourself
Think about the notes you scribble in the margin of a recipe β βuse less sugarβ or βthis takes longer than it says.β They help whoever reads it next, but they arenβt part of the recipe itself. In code, we have notes like that too β theyβre called comments. π
What is a comment?
A comment is a note written just for humans. The computer skips right over it and
never runs it. You make a comment by typing two slashes //. Everything after the
slashes on that line is your note.
// This is a comment. The computer ignores it!
let lives = 3; // You can also add a note at the end of a line.
Why write notes?
Code can be tricky to remember. A good comment explains why you did something, so that future-you (or a friend) understands it later. Itβs like leaving sticky notes for yourself! ποΈ
Look closely: the comments do not show up in the output! Only the println! line
printed. The notes were just for us to read while writing the code. π
println! line that says
// my favorite line. Press βΆ Run β notice your note
does NOT appear in the output!
Quick quiz
What happens to a line that starts with //?
Exactly! Comments are notes for humans, and the computer skips them. π
// and is a note for people, not the
computer. Comments don't print and don't change what your program does β they just help
you understand it. Next up: teaching your program to make choices! π