6/4/2020 Mr.
Sweeney's Course Wiki / Turing Reference - A First Program and Basic Output
Mr. Sweeney's Course Wiki Get a free wiki | Try our free business product log in help
Wiki Pages & Files Search this workspace
If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.
Social distancing? Try a better way to work remotely on your online files. Dokkio, a new product from PBworks, can help
your team find, organize, and collaborate on your Drive, Gmail, Dropbox, Box, and Slack files. Sign up for free.
VIEW
Turing Reference - A First Program and Basic Output Navigator
Turing
last edited by Steve Sweeney 6 years, 4 months ago Page history Turing Reference Conditional Statement
We begin our study of the Turing language with a simple program that outputs a message. Turing Reference - Data Types
Turing Reference - Logical Operators
% program: HelloWorld
Turing Reference - Selection Statement with Mu
%
% Outputs a simple message to the screen. Turing Reference - Selection Statement with Sin
Turing Reference - Selection Statement with Tw
put "Hello World!"
Pages Files options
% end program
SideBar
Looking at the various elements in detail: Winter 2019-2020
(Hint: Copy and paste this program into Turing, which provides colour-coding for the different MCR3U - Functions
categories of code) MDM4U - Data Management
Fall 2019-2020)
1. Comments: MFM2P - Grade 10 Mathematics, Applied Stream
MPM2D - Grade 10 Mathematics, Academic Stream
The % character indicates a comment in a Turing program. Anything to the right of a % is ignored MCR3U - Grade 11 Mathematics, Functions, University
Stream
by the computer. They are useful in understanding and planning your program, and they also help
anyone else who may be reading your code.
Winter Term 2018-2019:
MCR3U - Functions
ICS2O - Introduction to Computer Studies
2. % program: HelloWorld
ICS3C - Introduction to Computer Programming
ICS3U - Introduction to Computer Science
This is the name which identifies the program. The choice of name is up to the programmer, but ICS4C - Computer Programming
you should try to have a meaningful name, which gives some idea about the purpose of your ICS4U - Computer Science
program. The program name should also match the file name used to save the program.
Fall Term 2018-2019:
MHF4U - Advanced Functions
3. % Outputs a simple message to the screen. MDM4U - Data Management
ICS3C - Introduction to Computer Programming
ICS3U - Introduction to Computer Science
This comment is a more detailed description of the program. For simple programs, one or two
ICS4C - Computer Programming
sentences will be sufficient. Every program should have some sort of comment at the top giving a ICS4U - Computer Science
brief description.
Winter Term 2017-2018:
MCV4U - Calculus & Vectors
4. put "Hello World!" ICS2O - Introduction to Computer Studies
ICS3C - Introduction to Computer Programming
This is the only part of our program that does anything. ICS3U - Introduction to Computer Science
The statement put stands for output, and it will display characters on the screen. ICS4C - Computer Programming
ICS4U - Computer Science
After the put statement is a collection of characters surrounded by double quotes, "Hello World!".
This is known as a string. Fall Term 2017-2018
MHF4U - Advanced Functions
ICS3C - Introduction to Computer Programming
5. % end program
ICS3U - Introduction to Computer Science
ICS4C - Computer Programming
Another comment to indicate the end of our program. Although not strictly necessary in Turing, ICS4U - Computer Science
many languages require a defined beginning and end to a program. Better to establish good habits
now. All Previous Courses
Programs are not limited to printing (outputting) a single character string. Recent Activity
MCR3U 2019-2020 Sem2 Spring Term
% Program: PrintAddress edited by Steve Sweeney
[Link] Reference - A First Program and Basic Output 1/3
6/4/2020 Mr. Sweeney's Course Wiki / Turing Reference - A First Program and Basic Output
% Outputs a three-line address. MCR3U 2019-2020 Sem2 Spring Term
edited by Steve Sweeney
put "24 Sussex Drive"
MCR3U - Exp 08vt - Applications W2020.…
put "Ottawa, Ontario" uploaded by Steve Sweeney
put "K1M 1M4"
MCR3U 2019-2020 Sem2 Spring Term
edited by Steve Sweeney
% end program
MCR3U - Exp 04vt - Handout Transforma…
You may have noticed that the put command will output each string on a different line (which is called a uploaded by Steve Sweeney
new line in computer programming). You may wish to combine different types of output on a single
line. Consider the following program. MCR3U 2019-2020 Sem2 Spring Term
edited by Steve Sweeney
% Program: CombineLines
MCR3U - Exp 04 - Handout Transformati…
% uploaded by Steve Sweeney
% Outputs two messages: one on two lines, one combined into a single line
More activity...
put "Hello"
put "World!"
put "Goodbye" ..
put " " ..
put "World!"
% end program
It is also possible to print a blank line. We still use the put command, but instead of a collection of
characters, we output the empty string, "" (double quotes with nothing in between). Some extra
spaces have been used to provide simple formatting to the text.
% Program: DoubleSpace
%
% Output empty lines to double-space a message
put "This"
put ""
put " message"
put ""
put " is"
put ""
put " double-spaced"
% end program
So far, we have only output strings to the screen, which are surrounded by double-quotes. This raises
the question of how we could output an actual quote to the display. For example, you might read the
following passage in a book:
My favourite line from Shakespeare is, "To be or not to be, that is the question."
To have a program display this message, we need to be able to make the quotation mark, ", appear. In
Turing, the solution is to use another character, \, before the quotation mark. This is easier to
understand by looking at some sample code.
% Program: QuotationMarks
%
% Output quotation marks
put " a " % outputs the letter 'a', and 5 spaces on each side
put " \" " % outputs the double-quote, and 5 spaces on each
side
put "My favourite line from Shakespeare is, \"To be or not to be, that is
[Link] Reference - A First Program and Basic Output 2/3
6/4/2020 Mr. Sweeney's Course Wiki / Turing Reference - A First Program and Basic Output
the question.\""
% end program
This means Turing has two interpretations for the double-quote character (").
" The beginning or end of a string. Any characters between these quotes are for output.
\" The actual double-quote character. This is to be output to the display, just like typing the actual
character on your keyboard.
Comments (0)
You don't have permission to comment on this page.
Printable version
PBworks / Help About this workspace
Terms of use / Privacy policy / GDPR Contact the owner / RSS feed / This workspace is public
[Link] Reference - A First Program and Basic Output 3/3