CS110 Date: TA: Email: 09/22/2004 Kosta Kleisouris kkonst@paul.rutgers.edu, http://paul.rutgers.edu/~kkonst Chapter 2 1. In True Basic there are 2 types of variables: a. Variables for numbers, eg: X, Y, height, age b. Variables for strings, eg: name$, country$ 2. A string is a sequence of characters that is surrounded by quotes, e.g.: “Jim”, “USA”, “324”. So, variables for strings have a dollar sign at the end, whereas variables for numbers don’t. 3. Statements covered in chapter 2: REM, END, INPUT, READ/DATA, PRINT 4. REM: is used to insert remarks in a program a. You can also insert a remark at the end of a line, by typing an exclamation mark and then the remark 5. END: shows the end of a program. Every True basic program has to have and END statement 6. INPUT, INPUT PROMPT: they are used when we would like the user to supply some values. The syntax is as follows: a. INPUT variables separated by commas b. INPUT PROMPT “the prompt”: variables separated by commas 7. READ/DATA: they are used as a pair to assign values to variables. The difference between INPUT or INPUT PROMT and READ/DATA is that with the INPUT, INPUT PROMPT statements the program stops and asks the user to supply values for some variables, whereas with the READ/DATA the values are built into the program. The syntax is as follows: a. READ variables separated by commas DATA values separated by commas b. Common syntax errors: Not enough data, Wrong type of data 8. PRINT: is used to display information on the screen of the computer. The syntax is: a. PRINT list The list consists of numbers, strings, variables or mathematical expressions separated by commas or semicolons. Example #1: Prints employees’ checks REM Section C7 REM Name: John Smith REM Program: 2-10 REM Date: 06/07/04 REM TA name: Ann Patterson INPUT PROMPT “Give me your name”: name$ INPUT PROMPT “What is today’s date?”: dt$ INPUT PROMPT “What is your salary?”: salary PRINT dt$ PRINT “Pay “;name$;” a check made out for $”;salary END -----------OUTPUT ---------------------Give me your name Peter Rodriguez What is today’s date? 06/07/04 What is your salary? 50000 06/07/04 Pay Peter Rodriguez a check made out for 50000 Example #2 REM Shopping a bicycle READ shipping, tax DATA 20, 50 !shipping cost and taxes PRINT “How much does this bicycle cost? ”; INPUT cost PRINT “The total cost is “;cost+shipping+tax END ---------------- OUTPUT ----------------How much does this bicycle cost? 200 The total cost is 270 Chapter 3 1. So far, we have used INPUT, INPUT PROMPT and READ/DATA to assign values to variables. These statements don’t give us the flexibility to combine values in order to create new values. For instance, we might want to calculate some values based on some values that the user has supplied. A statement that does numerical calculations is LET. Syntax: LET variable-name = equation How it works: The equation is reduced down to a single value that is assigned to the variable on the left of the equal sign. If the equation contains other variables, these variables are replaced by their values. 2. Variable names consist of letters and numbers, but the first character of the variable name is always a letter. You can also use an underline ( _ ) in a variable name, but no space. Finally, all the statements of True Basic (READ, DATA, INPUT etc), can not be used as variable names. 3. How do we combine values in calculations in order to create new values? The following mathematical operations are available: Addition (+) Division (/) Subtraction (-) Exponentiation (^) Multiplication (*) Grouping () Example #1 READ pi, diam DATA 3.14, 10 LET circlearea = pi*(diam/2)^2 PRINT “The area of the circle is “;circlearea END Example #2 LET kount = 1 LET kount = kount + 4 LET kount = (kount – 2)/2 PRINT “The value of kount is “;kount END Example #3 READ apple_bcost, orange_bcost, banana_bcost DATA 1.3, 1.2, 1.1 ! dollars per pound that a store pays to buy a particular type of fruit LET apple_scost = apple_bcost*1.5 !selling cost of apples LET orange_scost = orange_bcost*1.5 !selling cost of oranges LET banana_scost = banana_bcost *1.5 !selling cost of bananas INPUT PROMPT “How many pounds of apples do you want ?” : apple_pounds INPUT PROMPT “How many pounds of oranges do you want ?”: orange_pounds INPUT PROMPT “How many pounds of bananas do you want ?”: banana_pounds LET cost = apple_pounds*apple_scost + orange_pounds*orange_scost + banana_pounds*banana_scost PRINT “Customer has to pay $“;cost END Example #4 INPUT PROMPT “What is the length of the room ?”:length INPUT PROMPT “What is the width of the room ?”:width LET roomarea = length*width PRINT “The area of the room is “;roomarea END Example #5 (2 Syntax Errors) LET K = 9 LET K – 1 = X LET Y = ( (X + 2)* 2/4 PRINT Y END Assignments for Next Week: 75-5, 89-6 Note for problem 89-6: the last sentence of this problem may lead you to believe a conditional IF-THEN statement is needed; you don’t need such a statement. The output is just a number and a statement.