0
0
Swiftprogramming~10 mins

Comments and documentation markup in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Comments and documentation markup
Start Code
Read Line
Is it a Comment?
NoExecute Code
Yes
Ignore or Use for Docs
Next Line
Back to Read Line
The program reads each line, checks if it's a comment or documentation markup, then either ignores it or uses it for docs, otherwise executes the code.
Execution Sample
Swift
/// Adds two numbers
func add(a: Int, b: Int) -> Int {
    // Return sum
    return a + b
}
This Swift code shows a documentation comment for a function and a normal comment inside the function.
Execution Table
StepLine ReadIs Comment?Comment TypeActionOutput
1/// Adds two numbersYesDocumentationStore for docs
2func add(a: Int, b: Int) -> Int {NoCodeExecute function declaration
3 // Return sumYesSingle-line commentIgnore during execution
4 return a + bNoCodeReturn sum of a and b
5}NoCodeClose function
💡 End of code reached, all comments processed or ignored, function declared.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
Function addundefinedDeclaredReady to callDeclared
Key Moments - 2 Insights
Why does the documentation comment start with /// instead of //?
/// marks a documentation comment that tools use to generate docs, while // is a normal comment ignored by tools. See step 1 in execution_table.
Does the single-line comment inside the function affect the program output?
No, single-line comments like // Return sum are ignored during execution, as shown in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what action is taken at step 1?
AExecute the code line
BIgnore the comment completely
CStore the documentation comment for docs
DReturn a value
💡 Hint
Check the 'Action' column at step 1 in the execution_table.
At which step is the function declared?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for 'Execute function declaration' in the Action column.
If the comment on step 3 was removed, what would change in the execution?
ANothing changes in execution
BThe output would change
CThe function would not work
DThe documentation would be missing
💡 Hint
Step 3 comment is ignored during execution, so removing it does not affect output.
Concept Snapshot
Swift comments:
// single-line comment ignored by compiler
/// documentation comment used by tools
/* multi-line comment */ for longer notes
Comments do not affect code execution but help explain code.
Full Transcript
This example shows how Swift reads code lines and identifies comments. Documentation comments start with /// and are stored for generating docs. Single-line comments with // are ignored during execution. The function is declared normally. Comments help explain code but do not change how it runs.