Comments in Swift
In this tutorial, we will learn how to write comments in Swift programming language. We will go through the different types of comments and how to use them in a program.
What are Comments
Comments in Swift are non-executable statements that are used to annotate the code, provide explanations, and make it more readable. They are ignored by the Swift compiler during compilation.
Syntax
The syntax to define comments in Swift is:
// Single line comment
/* Multi-line comment
continues here */
Example 1: Single line comments
- Single line comments in Swift start with
//
and continue to the end of the line. - They are used to add short explanations or notes within the code.
Swift Program
// Print Hello, World! to the console
print("Hello, World!")
Output
Hello, World!
Example 2: Multiline comments
- Multiline comments in Swift start with
/*
and end with*/
. - They can span multiple lines and are used for longer explanations or to comment out blocks of code.
Swift Program
/* Print Hello, World! to the console
This is a multi-line comment */
print("Hello, World!")
Output
Hello, World!