Swift Comments
A comment is a note written in code for people to read. The program ignores every comment.
A comment on its own line
The note above the print explains that line. Only the greeting reaches the screen.
// Print a greeting
print("Hello")A comment after code
The note after the code explains that print line. The printed words are the only output.
print("Hello again") // Print another greetingA block comment
The block note spans two lines above the prints. Both printed words still appear in order.
/*
Print two short lines
Each line shows one word
*/
print("First")
print("Second")Example
// Print the title
print("Morning")
/* Print the next line
The step prints below this note */
print("Tea")Key points
- Use // to start a line comment.
- A line comment runs to the end of its line.
- Use /* to open a block comment and */ to close it.
- A comment can sit above a line of code or after code on the same line.
In this example, a line comment sits above the first print, and that print shows Morning. A block comment sits above the second print, and that print shows Tea.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does a program do with a comment?