Difference between revisions of "Swift Live Reference"

From Coder Merlin
Line 5: Line 5:
|exerciseID=1
|exerciseID=1
|height=100
|height=100
|mode=swift
|language=swift
|initialCode=
|initialCode=
// single-line comments begin with two slashes
// single-line comments begin with two slashes
Line 14: Line 14:
|exerciseID=2
|exerciseID=2
|height=100
|height=100
|mode=swift
|language=swift
|initialCode=
|initialCode=
/* multi-line comments begin with slash-star
/* multi-line comments begin with slash-star
Line 27: Line 27:
|exerciseID=3
|exerciseID=3
|height=100
|height=100
|mode=swift
|language=swift
|initialCode=
|initialCode=
print("Hello, World!")
print("Hello, World!")
Line 36: Line 36:
|exerciseID=4
|exerciseID=4
|height=100
|height=100
|mode=swift
|language=swift
|initialCode=
|initialCode=
print("to", "ease", "another's", "heartache", separator: "*")
print("to", "ease", "another's", "heartache", separator: "*")
Line 45: Line 45:
|exerciseID=5
|exerciseID=5
|height=100
|height=100
|mode=swift
|language=swift
|initialCode=
|initialCode=
print("to", "ease", "another's", "heartache", separator: "*", terminator: "<>")
print("to", "ease", "another's", "heartache", separator: "*", terminator: "<>")
Line 64: Line 64:
|exerciseID=6
|exerciseID=6
|height=100
|height=100
|mode=swift
|language=swift
|initialCode=
|initialCode=
let gravity = 9.81 // meters per second per second
let gravity = 9.81 // meters per second per second
Line 75: Line 75:
|exerciseID=7
|exerciseID=7
|height=100
|height=100
|mode=swift
|language=swift
|initialCode=
|initialCode=
var currentTemperature = 23.1 // degrees celsius
var currentTemperature = 23.1 // degrees celsius
Line 89: Line 89:
|exerciseID=9
|exerciseID=9
|height=100
|height=100
|mode=swift
|language=swift
|initialCode=
|initialCode=
var n = 7
var n = 7

Revision as of 23:06, 23 January 2023

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]

CoderMerlin™ Code Explorer: W0000 (1) 🟢


Multi-line comments[edit]

CoderMerlin™ Code Explorer: W0000 (2) 🟢


Output[edit]

print[edit]

CoderMerlin™ Code Explorer: W0000 (3) 🟢


print with separator[edit]

CoderMerlin™ Code Explorer: W0000 (4) 🟢


print with terminator[edit]

CoderMerlin™ Code Explorer: W0000 (5) 🟢


Types[edit]

In Swift, it is customary to capitalize all types.

Int
A signed integer value type
Float
A single-precision, floating-point value type
Double
A double-precision, floating-point value type
String
A Unicode string value that is a collection of characters

Constant and Variable Declarations[edit]

Constant Declarations[edit]

Constants (named values which should never change) use the let keyword

CoderMerlin™ Code Explorer: W0000 (6) 🟢


Variable Declarations[edit]

Variables (named values which may change) use the var keyword

CoderMerlin™ Code Explorer: W0000 (7) 🟢


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]

CoderMerlin™ Code Explorer: W0000 (9) 🟢


else[edit]

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

switch[edit]

switch n {
    case 0:
        print("n is zero")
    case 1:
        print("n is one")
    default:
        print("n is neither one nor zero")
}

Loops[edit]

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

for[edit]

for loops are helpful when the number of iterations is known.

// From zero up to but excluding ten
for n in 0 ..< 10 {
    print(n)
}

// From one up to and including ten
for n in 1 ... 10 {
    print(n)
}

while[edit]

while loops are helpful when the condition may initially be false and the statements should not be executed.

while n < 10 {
    n += 1
}

repeat while[edit]

repeat while loops are helpful when the statements should be executed at least once.

repeat {
    n += 1
} while n < 10

References[edit]