Difference between revisions of "W1040 Printing and String Interpolation"
Jeff-strong (talk | contribs) m (Editorial review and minor corrections) |
|||
Line 3: | Line 3: | ||
* [[W1037 Expressions]] | * [[W1037 Expressions]] | ||
== Background == | == Background == | ||
A '''teletype machine''' (or teleprinter) is an electromechanical device | A '''teletype machine''' (or teleprinter) is an electromechanical device that can be used to send and received typed messages over a distance. It was one of the first devices that enabled the layman, not trained in Morse Code, to communicate easily with another. | ||
In addition to processing commands to print '''printable characters''' the teletype processed '''non-printable characters'''. The most common of these are: | In addition to processing commands to print '''printable characters''', the teletype processed '''non-printable characters'''. The most common of these are: | ||
; '''TAB''' : A control character (ASCII code 0x09) used to move a device's position to the next horizontal tab stop. | ; '''TAB''' : A control character (ASCII code 0x09) used to move a device's position to the next horizontal tab stop. | ||
; '''LINE FEED''' : A control character (ASCII code 0x0A) used to move a device's position to the next line. | ; '''LINE FEED''' : A control character (ASCII code 0x0A) used to move a device's position to the next line. Abbreviated as LF. | ||
; '''CARRIAGE RETURN''' : A control character (ASCII code 0x0D) used to reset a device's position to the beginning of a line of text. | ; '''CARRIAGE RETURN''' : A control character (ASCII code 0x0D) used to reset a device's position to the beginning of a line of text. Abbreviated as CR. | ||
== Introduction == | == Introduction == | ||
The '''print''' function is one of the most basic functions available and is | The '''print''' function is one of the most basic functions available and is one of the most useful. It allows us to easily output data from our program, most often to the console. As such, it is invaluable for both a completed programming and for helping us to debug a program during development. | ||
== Basic Usage == | == Basic Usage == | ||
In its most basic form, the ''print'' function takes a single argument. | In its most basic form, the ''print'' function takes a single argument. It's able to print integers, floating points, Booleans, and strings. | ||
{{CodeExplorer | {{CodeExplorer | ||
|exerciseID=1 | |exerciseID=1 | ||
Line 34: | Line 34: | ||
print(12, 13.0, true, "Water supply") | print(12, 13.0, true, "Water supply") | ||
}} | }} | ||
By default, each of the items is separated by a space character. | By default, each of the items is separated by a space character. Swift enables us to easily change this '''separator''' to any character (or characters) that we'd prefer: | ||
{{CodeExplorer | {{CodeExplorer | ||
|exerciseID=3 | |exerciseID=3 | ||
Line 44: | Line 44: | ||
== Mutliple Items on a Single Line == | == Mutliple Items on a Single Line == | ||
By default, Swift | By default, Swift inserts a new line at the end of each print statement. Consider: | ||
{{CodeExplorer | {{CodeExplorer | ||
|exerciseID=4 | |exerciseID=4 | ||
Line 109: | Line 109: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== String Interpolation == | == String Interpolation == | ||
String interpolation enables us to easily form complex strings by '''interpolating''' other values (literals, constants, variables, and expressions) into the string. | String interpolation enables us to easily form complex strings by '''interpolating''' other values (literals, constants, variables, and expressions) into the string. The values to be interpolated are enclosed within parentheses and preceded by a backslash. For example: | ||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
let a = 7 * 7 | let a = 7 * 7 | ||
Line 122: | Line 122: | ||
== Standard Output == | == Standard Output == | ||
[[File:Stdstreams-notitle.svg|thumb|left|link=|Standard Streams]] | [[File:Stdstreams-notitle.svg|thumb|left|link=|Standard Streams]] | ||
By default, a user's console output (nowadays | By default, a user's console output (nowadays a screen) is fed from a program through the '''standard output stream'''. This stream of data is established by the operating system and connected to the running program automatically. | ||
== Key Concepts == | == Key Concepts == |
Latest revision as of 20:07, 8 February 2023
Prerequisites[edit]
Background[edit]
A teletype machine (or teleprinter) is an electromechanical device that can be used to send and received typed messages over a distance. It was one of the first devices that enabled the layman, not trained in Morse Code, to communicate easily with another.
In addition to processing commands to print printable characters, the teletype processed non-printable characters. The most common of these are:
- TAB
- A control character (ASCII code 0x09) used to move a device's position to the next horizontal tab stop.
- LINE FEED
- A control character (ASCII code 0x0A) used to move a device's position to the next line. Abbreviated as LF.
- CARRIAGE RETURN
- A control character (ASCII code 0x0D) used to reset a device's position to the beginning of a line of text. Abbreviated as CR.
Introduction[edit]
The print function is one of the most basic functions available and is one of the most useful. It allows us to easily output data from our program, most often to the console. As such, it is invaluable for both a completed programming and for helping us to debug a program during development.
Basic Usage[edit]
In its most basic form, the print function takes a single argument. It's able to print integers, floating points, Booleans, and strings.
Mutliple Items[edit]
The print function can also print multiple items of various types.
By default, each of the items is separated by a space character. Swift enables us to easily change this separator to any character (or characters) that we'd prefer:
Mutliple Items on a Single Line[edit]
By default, Swift inserts a new line at the end of each print statement. Consider:
Swift enables us to easily change this terminator to any character (or characters) that we'd prefer:
Special Characters[edit]
We can use special characters to alter what we're printing:
- \t represents a Tab
- \n represents a Newline
- \r represents a Carriage return
Consider the newline:
Consider the carriage return:
Consider the tab:
Concatenation[edit]
Strings can be combined by using the + concatenation operator:
let s1 = "The"
let s2 = " Inner "
let s3 = "Light"
print(s1 + s2 + s3)
// Prints:
// The Inner Light
String Interpolation[edit]
String interpolation enables us to easily form complex strings by interpolating other values (literals, constants, variables, and expressions) into the string. The values to be interpolated are enclosed within parentheses and preceded by a backslash. For example:
let a = 7 * 7
let b = 4.0 * 4.0
let c = false
print("This is a string. 7 * 7 is \(a). 4.0 * 4.0 is \(b). The opposite of true is \(c).")
// Prints:
// This is a string. 7 * 7 is 49. 4.0 * 4.0 is 16.0. The opposite of true is false.
Coming Soon | |
|
Standard Output[edit]
By default, a user's console output (nowadays a screen) is fed from a program through the standard output stream. This stream of data is established by the operating system and connected to the running program automatically.
Key Concepts[edit]
Exercises[edit]
Exercises previously deferred:
- M1037-10 Complete Merlin Mission Manager Mission M1037-10.
- M1039-10 Complete Merlin Mission Manager Mission M1039-10.
- M1039-11 Complete Merlin Mission Manager Mission M1039-11.
References[edit]
- Teleprinter (Wikipedia)
- Print (Swift Documentation)
- Strings (Swift Documentation)