← Back to Swift Course | Chapter 1: Setup & Basics | Lesson 6 of 7

Comments in Swift

Comments are little notes you leave in your code for humans to read; the computer ignores them completely.

Single-Line Comments

A single-line comment starts with // and continues to the end of that line. The compiler ignores everything after // on that line entirely.

Example: Single-Line Comments

markup
// This prints a greeting
print("Hello, Swift!") // this part is also a comment

Multi-Line Block Comments

A block comment starts with /* and ends with */, and can span multiple lines, making it useful for longer explanations.

Example: Multi-Line Block Comments

markup
/*
 This program demonstrates
 multi-line block comments
*/
print("Block comments explained")

Nested Block Comments

Unlike C or Java, Swift allows block comments to be nested inside one another, which makes it easy to comment out a section of code that already contains a block comment.

Note: This is handy for temporarily disabling a chunk of code that already has comments inside it.

Example: Nested Block Comments

markup
/* outer comment /* nested inner comment */ still outer */
print("Swift supports nested block comments")
Common Mistakes
  1. Believing comments affect how the program runs; the compiler skips them entirely.
  2. Using comments to explain obvious code instead of explaining *why* a non-obvious decision was made.
  3. Forgetting that block comments /* */ can be nested in Swift, unlike in many other C-family languages, which can cause confusion when mixing styles.
Chapter Summary
  • // starts a single-line comment that runs to the end of the line.
  • /* ... */ marks a multi-line block comment.
  • Swift uniquely allows nested block comments (/* /* ... */ */).
  • Good comments explain *why* code does something, not just *what* it does.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.