Difference between revisions of "Swift Live Reference"
Jeff-strong (talk | contribs) m (Editorial review and minor corrections) |
|||
Line 1: | Line 1: | ||
== Comments == | == Comments == | ||
Humans use ''comments'' to document code for other humans. They have no effect on the program's operation itself. | |||
=== Single-line comments === | === Single-line comments === | ||
{{CodeExplorer | {{CodeExplorer | ||
Line 59: | Line 59: | ||
== Constant and Variable Declarations == | == Constant and Variable Declarations == | ||
=== Constant | === Constant declarations === | ||
Constants (named values | Constants (named values that should never change) use the '''let''' keyword. | ||
{{CodeExplorer | {{CodeExplorer | ||
|exerciseID=6 | |exerciseID=6 | ||
Line 71: | Line 71: | ||
=== Variable Declarations === | === Variable Declarations === | ||
Variables (named values | Variables (named values that could change) use the '''var''' keyword. | ||
{{CodeExplorer | {{CodeExplorer | ||
|exerciseID=7 | |exerciseID=7 | ||
Line 84: | Line 84: | ||
Control structures are used to alter the otherwise sequential flow of a program. | Control structures are used to alter the otherwise sequential flow of a program. | ||
=== Conditionals === | === Conditionals === | ||
Conditionals are used to execute a segment of code | Conditionals are used to execute a segment of code if a condition is met. | ||
==== if ==== | ==== if ==== | ||
{{CodeExplorer | {{CodeExplorer | ||
Line 119: | Line 119: | ||
Loops are used to repeat a segment of code until a condition is met. | Loops are used to repeat a segment of code until a condition is met. | ||
==== for ==== | ==== for ==== | ||
'''for''' | The '''for''' loop is helpful when the number of iterations is known. | ||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
// From zero up to but excluding ten | // From zero up to but excluding ten | ||
Line 126: | Line 126: | ||
} | } | ||
// From one up to and including | // From one up to and including 10 | ||
for n in 1 ... 10 { | for n in 1 ... 10 { | ||
print(n) | print(n) | ||
Line 133: | Line 133: | ||
==== while ==== | ==== while ==== | ||
'''while''' | The '''while''' loop is helpful when the condition might initially be false and the statements should not be executed. | ||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
while n < 10 { | while n < 10 { | ||
Line 141: | Line 141: | ||
==== repeat while ==== | ==== repeat while ==== | ||
'''repeat while''' | The '''repeat while''' loop is helpful when the statements should be executed ''at least'' once. | ||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
repeat { | repeat { |
Latest revision as of 12:14, 11 February 2023
Comments[edit]
Humans use comments to document code for other humans. They have no effect on the program's operation itself.
Single-line comments[edit]
Multi-line comments[edit]
Output[edit]
print[edit]
print with separator[edit]
print with terminator[edit]
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 that should never change) use the let keyword.
Variable Declarations[edit]
Variables (named values that could change) use the var keyword.
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 if a condition is met.
if[edit]
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]
The for loop is 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 10
for n in 1 ... 10 {
print(n)
}
while[edit]
The while loop is helpful when the condition might initially be false and the statements should not be executed.
while n < 10 {
n += 1
}
repeat while[edit]
The repeat while loop is helpful when the statements should be executed at least once.
repeat {
n += 1
} while n < 10