Difference between revisions of "W1161 Basic Input"

From Coder Merlin
Line 35: Line 35:


If we typed {{Key|A}} {{Key|B}} {{Key|C}} {{Key|D}} {{Key|ENTER}} then ''line'' would contain "ABCD".  If instead we typed {{Key|1}} {{Key|2}} {{Key|3}} {{Key|ENTER}} then ''line'' would contain "123".  And if instead we typed only {{Key|Enter}} then line would contain "", an empty string.
If we typed {{Key|A}} {{Key|B}} {{Key|C}} {{Key|D}} {{Key|ENTER}} then ''line'' would contain "ABCD".  If instead we typed {{Key|1}} {{Key|2}} {{Key|3}} {{Key|ENTER}} then ''line'' would contain "123".  And if instead we typed only {{Key|Enter}} then line would contain "", an empty string.
But note that the return type from readLine() is ''String?'', an ''optional'' string.  Under what circumstances would the return value be nil?


== Syntactic Sugar ==
== Syntactic Sugar ==

Revision as of 11:47, 22 March 2020

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
IBM 1402 Card Punch Input

Prerequisites[edit]

Introduction[edit]

The programs that we've written so far have not involved input provided by humans. Either the data were hard-coded before the program was executed or they were established immediately prior to execution by  Merlin Mission Manager . Hard-coded data means that the data in question is embedded in the program as "code" and consequently cannot be changed without altering the program itself. While there are some circumstances in which this is sufficient (consider physical constants such as the gravitational constant G or speed of light c) in most cases this results in a program of very limited usefulness. (Consider a calculator without any buttons that only adds the predetermined numbers 123 and 456.) Input, perhaps provided by a keyboard and/or mouse, empower programs with the ability to calculate answers to specific problems that a human is encountering. Nearly all programs with which you interact involve input provided by humans.

ObserveObserveIcon.png
Observe, Ponder, and Journal: : Section 1
  1. List three programs which you commonly use for which you provide input.
  2. List one program which you commonly use for which you provide no input.
  3. List three input devices, excluding a mouse and keyboard, that you commonly use to interact with electronic devices.

Keyboard[edit]

Teletype

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. It included a keyboard for use by a human operator. In addition to the expected alphabetic and numeric characters, it contained a series of non-printable characters to control the conversation.
The most common of these are:

END OF TRANSMISSION/FILE
A control character (ASCII code 0x04) used to indicate the end of transmission or end of file. Abbreviated EOT or EOF.
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.
ObserveObserveIcon.png
Observe, Ponder, and Journal: : Section 2
  1. Which button(s) is/are used on a calculator to indicate that you've completed an expression and want the calculator to evaluate the expression and provide an answer?
  2. Which button(s) is/are used on a calculator to indicate that you've completed a number?

Reading a Single Line of Text[edit]

The readLine() function enables us to (unsurprisingly) read a single line of text from the console. The signature is:

func readLine() -> String?

All of the characters entered up to (but by default excluding) the newLine character will be returned. For example, given the code:

let line = readLine()

If we typed A B C D ENTER then line would contain "ABCD". If instead we typed 1 2 3 ENTER then line would contain "123". And if instead we typed only Enter then line would contain "", an empty string.

But note that the return type from readLine() is String?, an optional string. Under what circumstances would the return value be nil?

Syntactic Sugar[edit]

Reading Multiple Lines of Text[edit]

Key Concepts[edit]

Exercises[edit]

References[edit]