Introduction to TURING Comments Some code is difficult to understand, even if you understand the language it is written in. To that end, the designers of programming languages have allowed us to comment our code. In Turing, this is done by placing the percent sign (%) before a comment. % This is a comment. Everything after the % on this line is ignored by the computer. Another way of commenting code is by using matching sets of "/*" and "*/" /* This is a comment. Everything after the forward-slash-asterisk but before the matching asterisk-forward-slash is ignored by the computer. */ Output We want to put some text on the screen, to prove that the link between us and the computer does in fact exist. Let me repeat that again: We want to put some text on the screen. In Turing, we do this by using the put command, followed by the text, contained in double quotes, that we want to output, like this: put "Hello World" Variable Types Strings: Characters: Integers: We have already encountered the string. Now let us answer the unrelenting question, "What is a string?" A string is a sequence of characters. The characters can be ordinary things such as letters and numbers, or they can be wacky things found in the ASCII chart such as the plus-minus sign (à ±). A string is bounded by quotation marks, as in "Yarr! I caught me aye fish!" A string can contain a maximum of 255 characters. A character is a single letter, number, or any wacky symbol that you can find in the ASCII chart. Characters are bound by quotation marks, as in 'Q'. Generally, strings use double quotes and characters use single quotes, though either type can use either style of quotation mark. We should all know what an integer is, given some fundamental math. Just to make sure, an integer is a whole number; it has no decimals; it is a fraction whose denominator is 1; it can be negative, zero, or positive. Real Numbers:
Boolean: Real numbers can contain decimal places. Turing supports up to 16 decimal places for real numbers. Real numbers contain the realms of the positive, the negative, and zero. The boolean (named after George Boole) variable type contains only two alternatives: true or false. We will look into these in greater depth later, particularly when learning about conditions or if statements. Returning to Output Now that we know our Hello World program output a string on the screen, let's experiment with the other variable types. Let's try outputting an integer, then a real number. put 7 % Outputs 7 put 8.19538032154 % Outputs 8.19538 Notice how the real number was rounded. Now I know everyone loves arithmetic, so it's about time we did some. Quickly now, (4 + 8) / 2 * (3-5) equals what? put (4 + 8) / 2 * (3-5) % Outputs -12 Turing does the math following the precedence rules, BEDMAS (Brackets, Exponents, Division and Multiplication, Addition and Subtraction). Now let's output the question and the answer: put "(4 + 8) / 2 * (3-5) = ", (4 + 8) / 2 * (3-5) % Output: (4 + 8) / 2 * (3-5) = -12 Notice the use of the comma (,). The comma separates one section from the next. The first section is a string, which shows us the question. The next section is an integer, which is the answer. Strings can be added together, a process called concatenation: we are taking one value and placing another value directly after it. put "Hello " + "Alan Turing." % Outputs "Hello Alan Turing." Turing 001 Put The put command is used to print information to the screen. put "Hello World!"...prints Hello World! (without the quotes) to the screen. Write a program that prints your first name to the screen. Save as "001.t". Turing 002 Put II
Type in the following program, then identify and fix any errors: put "Hi, I am a PENTIUM" "put Hi, I am a PENTIUM" put Hi, I am a PENTIUM Correct the errors and save as "002.t" Turing 003 Program Header All programs should include a program header. This header should, as a minimum, include a program description, the author s (programmer s) name, the date, and the filename. An example header is shown below: % author: Jane Doe % date: 2003-09-06 % filename: put2.t % description: prints user s name to the screen Note that a percent sign ( % ) is used before each of the header lines. These are used to tell the Turing program to ignore these lines and treat them like comments. For most programs written in this class, you may copy and paste my requirements into the "description" part of the program header. If it is too wide to fit on the screen, break it up into two or more lines. Add an appropriate program header to 001.t. Save as "003.t". Turing 004 Concatenation By appending two periods (.. ) to a put statement, the next put statement will print its output on the same line as the first statement. Example: put "tom".. put "boy"...prints "tomboy" on the screen. Add a line to 003.t that prints your last name after your first name. Be sure it prints on the same line. Add another line to your program so there is a space between your first name and your last name. Save as "004.t". Turing 005 Addition
The addition operator in Turing is the plus sign ("+"). The following statement adds the numbers 3 and 4: put 3 + 4 Write a program that prints the sum of 5, 6, and 7. Save as "005.t". Turing 006 Addition II Write a program that prints the sum of 5.123 and 6.29. Save as "006.t". Turing 007 Addition III Modify 006.t so that there are two put statements. The first puts the two values to be added, and the second does the math. The output should look similar to "The sum of and is. ". Save as "007.t". The subtraction operator in Turing is a hyphen ( - ). Turing 008 Subtraction The following statement subtracts the number 3 from 4: put 4-3 Write a program that subtracts 5 from 6. Save as "008.t". Turing 009 Multiplication
The multiplication operator in Turing is an asterisk ("*"). The following statement multiplies the numbers 3 and 4: put 3 * 4 Write a program that prints the product of 5 and 6. Save as "009.t". Turing 010 Division The division operator in Turing is a slash ( / ). The following statement divides the number 3 by 4: put 3 / 4 Write a program that prints the quotient of 5 divided by 6. Save as "010.t". Turing 011 Exponents The exponent operator in Turing is two asterisks ( ** ). The following statement calculates the square of 3 (3 2 ): put 3 ** 2 Write a program that prints the answer of 2 to the exponent 8. Save as "011.t".
Turing 012 Order of Operation Remember from math class that 3 + 2 X 5 = 13, not 25. The same holds true for Turing. Even though Turing will calculate algebraic equations properly, it is good programming practice to group items into their intended order of operation by surrounding them with parentheses (round brackets). For example, the above equation, written with a Turing put statement, is better written as: put 3 + (2 * 5) A more complex equation may be written as: put 12 + ( ( 4-6 ) / 3 ) Things to remembers: Turing uses '*' for multiplication, not 'x' Turing uses '**' for exponentiation Turing only uses parentheses (round brackets), not square brackets Brackets can be nested (inside each other), and it is strongly encouraged if it makes the statement clearer Write a program that prints the equivalent of each of the following algebraic statements to the screen: 2 + 4 x 5 2 x 6-4/2 8 + 2 (4+9/3) 3[(4+12)-2(3-1)] 23 + 42-6/3 Write each Turing statement so the output looks like the following: 2 + 4 x 5 = 22...where "22" is calculated with your programmed formula. Save as "012.t".