Start learning free

LESSON 3 OF 349 · WELCOME TO SWIFT

Comments in Swift in Swift

Lesson

TL ; DR:

  • Comments are lines of code that Swift will intentionally ignore (not execute). They don't do anything.


  • You add a single line comment in Swift by typing // followed by any text:

    // This is a comment!
    print("This is not!")
    


Full lesson:

Comments are lines of code that Swift will intentionally ignore. They don't do anything.

They're just used to create notes for yourself and others about what the code does.

There are two types of code comments in Swift:


1. Single line comments:

We create single line comments by typing // and Swift will ignore (not execute) any text between // and the end of the line.

Single line comments automatically end at the next line of code:

// Next line will greet the user:
print("Hello user!")

Output of this code:

Hello user!

We can also use single line comments at the end of a line to explain the code:

print("Hello user!") // This line greets the user
print("This is not a comment!")

Output of this code:

Hello user!
This is not a comment!

2. Multi-line comments

We can also comment multiple lines of code using multi-line comments.

We type /* to begin the comment and type */ to end the comment:

/* This is a
multi-line comment
print("This will not run")
Next line will greet the user:
*/

print("Hello user!")

Output of this code:

Hello user!

Assignment
Follow the Coding Tutorial and let's write some comments!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    Comments in Swift

    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free