Variables and Constants Variables: In order for a program to interact with its user, the user needs to be able to input data into the computer. The data inputted into the computer by users are stored in variables. Variables can store many types of information, including strings (text), numbers (integers, real numbers, etc.), and Booleans (yes/no). Declaring variables in Turing: var name-of-variable : type-of-variable Example: var name : string In plain English, this reads as: “The string will be stored in the variable named name”. var age : int This reads as: “A variable called age will hold an integer”. Variable naming rules: Variable names can contain letters (and technically numbers, but I won’t allow it). Variables cannot contain special characters, such as spaces, &,.- etc. There are a set of reserved words in Turing (ie: var, char, int, string, etc.) that cannot be used as variable names. Sample Variable names: Variable Declaration Good/Bad int age : var var firstName : string var 1stName : string var first name : string var first_name : string var age : integer var age : int Common Errors with Variables: 1. Variables must be declared before they are used. 2. Variables declared must be identical throughout the program. For example: firstName is not the same as firstname and is not the same as first_Name 3. Errors in the type of input. Examples: a. Enter an integer: 3.45 this will give an error, as integers do not have decimals. b. Enter a number: twenty this will also give an error. c. Enter your name: 6 is this an error? What do you think? Constants: Constants are much like variables in that they are used to store data for use in the program. The difference remains though, that constants do not change. The language is also slightly different in declaring a constant. Sample declaration: const pi : real := 3.14159 This means that pi can be called anywhere in the program for a calculation (instead of having to type 3.14159 every time pi is used). Assigning Values to variables: In order to assign values to variables from the user, we use the get statement. Try the following program: const pi : real := 3.14159 var radius : real var area : real put "Enter the radius " .. get radius % ask the user for input % get the radius from user area := pi * radius**2 % calculate the area put "The area is ", area % display results /* notice how keywords are bolded, quotes are in red, variables and constants are in blue, comments are green, and the rest is plain text. */ Practice Exercises: Save the following exercises in your notes as Variables#.t ie: Variables1.t, Variables2.t, etc. 1. There are 2.54 centimetres in one inch. Create a program that converts your height in inches to centimetres. Use a constant for the conversion factor. Prompt the user for their height in inches. Be sure to provide a clear output statement. 2. Write a program that greets a user. Ask the user for their name. Provide a statement such as: “Hello Bob!”. Use a constant for the greeting. Use a variable to store the user’s name. 3. Create a program that calculates the income for a school dance. The tickets are $15.00 each and costs came to $2000.00. Ask the user to input the number of people who attended the dance. Calculate the income and the profit. Display to the user the income and profit amounts. 4. A student wrote 5 tests. Ask the student for their name and then what each test is marked out of and what they got on the test. At the end, calculate the percentage for each test and calculate the overall percentage for the five tests. When querying the student, be sure to include their name in the statement. Make sure also, that the name is used during the output statements as well. http://www.beens.org/turing/index.html 5. Complete practice problems 015 – 022b