Swift Live Reference

From Coder Merlin
Revision as of 17:33, 5 March 2022 by Guanyu-su (talk | contribs) (Added basic page structure.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Comments[edit]

Comments are used by humans to document code for other humans. They have no effect on the program itself.

Single-line comments[edit]

// single-line comments begin with two slashes

Multi-line comments[edit]

/* multi-line comments begin with slash-star
   and continue 
   and continue
   until a closing star-slash */

Control Structures[edit]

Control structures are used to alter the otherwise sequential flow of a program.

Conditionals[edit]

Conditionals are used to execute a segment of code based upon a condition being met.

if[edit]

if n < 10 {
    print("n is less than 10")
}

else[edit]

if n < 10 {
    print("n is less than 10")
} else {
    print("n is not less than 10")
}

switch[edit]

Loops[edit]

Loops are used to repeat a segment of code until a condition is met.

for[edit]

for n in 0 ..< 10 {
    print(n)
}

while[edit]

while n < 10 {
    n += 1
}

repeat while[edit]

repeat {
    n += 1
} while n < 10