Difference between revisions of "W1040 Printing and String Interpolation"

From Coder Merlin
m (Editorial review and minor corrections)
 
(9 intermediate revisions by 2 users not shown)
Line 3: Line 3:
* [[W1037 Expressions]]
* [[W1037 Expressions]]
== Background ==
== Background ==
A '''teletype machine''' (or teleprinter) is an electromechanical device which can be used to send and received typed messages over a distance. It was one of the first devices which enabled the layman, not trained in Morse Code, to communicate easily with another.
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. Abbreviated as LF.
; '''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.
; '''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 also one of the most useful. It provides us with the ability to easily output data from our program, most often to the console. As such, it is invaluable for both a completed programming and for assisting us to debug a program during development.
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. It's able to print integers, floating points, Booleans, and strings.
In its most basic form, the ''print'' function takes a single argument. It's able to print integers, floating points, Booleans, and strings.
<syntaxhighlight lang="swift">
{{CodeExplorer
|exerciseID=1
|height=130
|language=swift
|initialCode=
print(12)    // An Int
print(12)    // An Int
print(13.0)  // A Double
print(13.0)  // A Double
print(true)  // A Boolean
print(true)  // A Boolean
print("Water supply") // A String
print("Water supply") // A String
</syntaxhighlight>
}}
 
== Mutliple Items ==
== Mutliple Items ==
The print function can also print multiple items of various types.
The print function can also print multiple items of various types.
<syntaxhighlight lang="swift">
{{CodeExplorer
|exerciseID=2
|height=50
|language=swift
|initialCode=
print(12, 13.0, true, "Water supply")  
print(12, 13.0, true, "Water supply")  
// Prints: 12 13.0 true Water supply
}}
</syntaxhighlight>
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:
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
<syntaxhighlight lang="swift">
|exerciseID=3
print(12, 13.0, true, "Water supply", separator:"|")  
|height=50
// Prints: 12|13.0|true|Water supply
|language=swift
</syntaxhighlight>
|initialCode=
print(12, 13.0, true, "Water supply", separator:"{{Bar}}")  
}}
 
== Mutliple Items on a Single Line ==
== Mutliple Items on a Single Line ==
By default, Swift will insert a new line at the end of each print statement. Consider:
By default, Swift inserts a new line at the end of each print statement. Consider:
<syntaxhighlight lang="swift">
{{CodeExplorer
|exerciseID=4
|height=130
|language=swift
|initialCode=
print("First")
print("First")
print("Second")
print("Second")
print("Third")
print("Third")
print("Fourth")
print("Fourth")
// Prints:
}}
// First
 
// Second
// Third
// Fourth
</syntaxhighlight>
Swift enables us to easily change this '''terminator''' to any character (or characters) that we'd prefer:
Swift enables us to easily change this '''terminator''' to any character (or characters) that we'd prefer:
<syntaxhighlight lang="swift">
{{CodeExplorer
|exerciseID=5
|height=130
|language=swift
|initialCode=
print("First", terminator:"-")
print("First", terminator:"-")
print("Second", terminator:"-")
print("Second", terminator:"-")
print("Third", terminator:"-")
print("Third", terminator:"-")
print("Fourth")
print("Fourth")
// Prints:
}}
// First-Second-Third-Fourth
 
</syntaxhighlight>
== Special Characters ==
== Special Characters ==
We can use special characters to alter what we're printing:
We can use special characters to alter what we're printing:
Line 59: Line 74:
* \r represents a Carriage return
* \r represents a Carriage return
Consider the newline:
Consider the newline:
<syntaxhighlight lang="swift">
{{CodeExplorer
|exerciseID=6
|height=50
|language=swift
|initialCode=
print("Hello\nWorld")
print("Hello\nWorld")
// Prints:
}}
// Hello
// World
</syntaxhighlight>
Consider the carriage return:
Consider the carriage return:
<syntaxhighlight lang="swift">
{{CodeExplorer
|exerciseID=7
|height=50
|language=swift
|initialCode=
print("Hello Dear\rWorld")
print("Hello Dear\rWorld")
// Prints:
}}
// World Dear
</syntaxhighlight>
Consider the tab:
Consider the tab:
<syntaxhighlight lang="swift">
{{CodeExplorer
|exerciseID=8
|height=50
|language=swift
|initialCode=
print("Hello\tDear\tWorld")
print("Hello\tDear\tWorld")
// Prints:
}}
// Hello  Dear    World
 
</syntaxhighlight>
== Concatenation ==
== Concatenation ==
Strings can be combined by using the '''+''' concatenation operator:
Strings can be combined by using the '''+''' concatenation operator:
Line 88: 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.   The values to be interpolated are enclosed within parentheses and preceded by a backslash. For example:
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 101: 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 to 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.
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 ==
== Exercises ==
== Exercises ==
{{W1040-Exercises}}
{{Exercises|
Exercises previously deferred:
<hr/>
# {{MMMAssignmentNotPrimary|M1037-10}}
# {{MMMAssignmentNotPrimary|M1039-10}}
# {{MMMAssignmentNotPrimary|M1039-11}}
}}


== References ==
== References ==

Latest revision as of 20:07, 8 February 2023

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Teletype

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.

CoderMerlin™ Code Explorer: W0000 (1) 🟢


Mutliple Items[edit]

The print function can also print multiple items of various types.

CoderMerlin™ Code Explorer: W0000 (2) 🟢

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:

CoderMerlin™ Code Explorer: W0000 (3) 🟢


Mutliple Items on a Single Line[edit]

By default, Swift inserts a new line at the end of each print statement. Consider:

CoderMerlin™ Code Explorer: W0000 (4) 🟢


Swift enables us to easily change this terminator to any character (or characters) that we'd prefer:

CoderMerlin™ Code Explorer: W0000 (5) 🟢


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:

CoderMerlin™ Code Explorer: W0000 (6) 🟢

Consider the carriage return:

CoderMerlin™ Code Explorer: W0000 (7) 🟢

Consider the tab:

CoderMerlin™ Code Explorer: W0000 (8) 🟢


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.
ComingSoonIcon.png
Coming Soon
  • More examples of string interpolation, including expressions and dealing with optional nils.

Standard Output[edit]

Standard Streams

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]

ExercisesExercisesIcon.png

Exercises previously deferred:


  1.  M1037-10  Complete  Merlin Mission Manager  Mission M1037-10.
  2.  M1039-10  Complete  Merlin Mission Manager  Mission M1039-10.
  3.  M1039-11  Complete  Merlin Mission Manager  Mission M1039-11.

References[edit]