Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables 1. What is a variable? Something whose value can change over time This value can change more than once over the time period (no limit!) Example:

Similar presentations


Presentation on theme: "Variables 1. What is a variable? Something whose value can change over time This value can change more than once over the time period (no limit!) Example:"— Presentation transcript:

1 Variables 1

2 What is a variable? Something whose value can change over time This value can change more than once over the time period (no limit!) Example: ▫At the start of a program: x = 1 ▫In the middle of the program: x = 5 ▫At the end of the program: x = 0 2

3 Why do we need variables? The exact same program/code can run with different results ▫interactive vs. non-interactive ▫The difference between a movie and a video game Programs are easier to read ▫Variables can have meaningful names ▫Numbers don’t have much meaning to us beyond their value 3

4 3 Parts to a Variable 1)The variable type 2)The variable identifier 3)The variable value 4

5 Understanding Variables as Pictures A variable is like a box in a computer’s memory The type of variable is like the shape of the box. The variable identifier is like a label on the box The value is what is stored inside the box (string) name Ovechkin 23 5

6 Common Data Types in Turing int Whole numbers real Decimal numbers boolean true or false string Sequence of characters e.g. “Mr. Lane”, “Computer” 6

7 Data Type Value Ranges string: Essentially any length of text boolean: true or false int: -2147483648  2147483647 real: -1.79769313486232 x 10 308  1.79769313486232 x 10 308 7

8 Using Variables – Identifier Rules Identifiers ▫Can contain numbers but cannot start with one  E.g. number1 is valid, 1number is NOT valid ▫Can contain underscores, _ ▫Cannot contain any spaces ▫Cannot contain special characters, e.g. %, @, *, -, “ ▫Cannot be reserved words, e.g. int, put, View, etc. 8

9 Using Variables – Identifier Conventions Conventions are what is expected, but are not rigid rules Identifiers ▫Should describe the variable’s purpose ▫Should use camel-case. This means that for a variable identifier with multiple words, each word except the first starts with a capital letter. E.g., studentNumber, myFavouriteColour 9

10 Using a Variable The value can be any expression as long as it matches the same type as the identifier Expression: Something that results in a value E.g. (5 + 4), “bob”, userAge + birthYear 10 1)Declare the type and identifier var : 2) Assign the value := operator :=

11 Assignment Statements 11 Give a value to a variable. Use the assignment operator ( := ) The variable to be assigned a value is placed on the left-hand side of the := operator. The value to be assigned is placed on the right- hand side of the := operator. The value can be a constant, a literal, another variable, or an expression.

12 Assigning Variables to Other Variables Just like assigning a variable to an expression we follow the same steps E.g. var twinBrotherAge : int var twinSisterAge : int twinBrotherAge := 15 twinSisterAge := twinBrotherAge How does a computer handle this last command? 12

13 Using a Variable Examples %Variable declarations and assignments var sum : int% Declaration sum := 4% Variable assignment %What exactly is the line below doing? sum := sum + 1% Variable assignment 13

14 A Shortcut Most times you know what the initial (starting) value of a variable should be when you create it. Instead of declaring and then assigning a variable you may put these two lines into one Example: var name : string := “Mr. Lane” var sum : int := 0 14

15 Operators OperatorExampleDescription +Price + Tax Adds two values -Cost - Discount Subtracts one value from another *Hours * Wage Multiplies two values /AnnualSalary / 12 Divides one value by another, remember you cannot store a decimal into an int +FirstName + LastName Joins / Concatenates two strings div5 div 2 Divides an integer by the second number and truncates (cuts off) all decimal values 15

16 Increment and Decrement Operators Increment means to increase a value by a consistant amount Conversely Decrement means to decrease a value by a consistant amount E.g. To increment a variable named count by 3 count := count + 3 NOTE: we need to add the increment value to the current value of count to get the new result 16

17 Expressions Examples 17 A statement that returns a value when executed ExpressionEvaluates to trueReturns true 5Returns 5 1 + 3Returns 4 8.3 * 9Returns 74.7 “Mr. ” + “Lane”Returns “Mr. Lane”

18 18 Expressions A statement that returns a value when executed Examples %alphabet = “ABCDE”; var alphabet : string alphabet := “A” + “B” + “C” + “D” + “E” %final value of year is “The year is 2009”; var year : string year := “The year is ” + 2009;

19 Expressions You should NEVER do calculations in an output statement. Instead assign the result of a calculation into a variable then output the variable Example: var sum : int := 0; sum := 10.3 + 4.2 + 3.7 + 6.5 var average : real := sum/4 put average 19

20 Expressions 20 ExpressionEvaluates to 490 + “ York Mills Road”“490 York Mills Road” var maker : string := “Aston Martin” var model : string := “Vanquish” var car : string := maker + “ ” + model%“Aston Martin Vanquish”

21 Using a Variable Examples % Variable declarations and assignments var isSkyBlue : boolean%var : isSkyBlue := true% Variable assignment % := NOTE: We only declare the variable once then we can assign and access the data as much as we want for the rest of the program! 21

22 Using a Variable Examples % Declaration and assignment var myMark : real myMark := 99.999 put myMark%outputs 99.999 myMark := myMark / 3%Assignment put myMark%outputs 33.333 22

23 Constants Constants are similar to variables, except that you provide a value for the constant when you declare it, and its value can never change const : := We cannot assign new values, but can use them like normal variables 23

24 Constants Same identifier rules as for variables ▫No spaces, special characters ▫Cannot start with a number ▫Cannot be a reserved word Conventions are different ▫All uppercase letters ▫Instead of camel-case, separate all words by an underscore ▫E.g., THIS_IS_A_CONSTANT_IDENTIFIER 24

25 Discussion Why do we have variables with different sizes? ▫More efficient space usage in memory – think differently sized aircraft, the right size for the job ▫Importance depends on size of program 25


Download ppt "Variables 1. What is a variable? Something whose value can change over time This value can change more than once over the time period (no limit!) Example:"

Similar presentations


Ads by Google