Our own code can quickly become difficult to understand when we return to it — sometimes only a few hours later! For this reason, it’s often useful to leave a note or reminder in our code for our future selves or other developers.
Comments can explain what the code is doing, leave instructions for developers using the code, or add any other useful annotations.
There are two types of code comments in Swift:
A single line comment will comment out a single line and is denoted by two forward slashes
//preceding it:// Prints "hi" print("hi")
You can also use a single line comment after a line of code:print("hi") // Prints "hi"
A multiline comment will comment out multiple lines and is denoted by
/*to begin the comment, and
*/to end the comment:/* This is all commented out. print("hi") None of this is going to run! */
Let’s practice adding a comment.
Create a single line comment with
// followed by your favorite emoji.
And then type the following:
What do you think this program will output?