Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort...

33
Variables Damian Gordon

Transcript of Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort...

Page 1: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

VariablesDamian Gordon

Page 2: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• We know what a variable is from maths.• We’ve all seen this sort of thing in algebra:

2x – 10 = 02x = 10

X = 5

Page 3: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• We know what a variable is from maths.• We’ve all seen this sort of thing in algebra:

2x – 10 = 02x = 10

X = 5

Page 4: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• We know what a variable is from maths.• We’ve all seen this sort of thing in algebra:

2x – 10 = 02x = 10

X = 5

Page 5: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• We know what a variable is from maths.• We’ve all seen this sort of thing in algebra:

2x – 10 = 02x = 10

X = 5

Page 6: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• We know what a variable is from maths.• We’ve all seen this sort of thing in algebra:

2x – 10 = 02x = 10

X = 5

Page 7: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• …and in another problem we might have:

3x + 12 = 03x = -12X = -4

Page 8: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• So a variable contains a value, and that value changes over time.

Page 9: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• …like your bank balance!

Page 10: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• In programming, we tell the computer the value of a variable• So, for example,

x <- 5

means “X gets the value 5” or “X is assigned 5”

Page 11: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• In programming, we tell the computer the value of a variable• So, for example,

x <- 5;

means “X gets the value 5” or “X is assigned 5”

Page 12: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• In programming, we tell the computer the value of a variable• So, for example,

x <- 5;

means “X gets the value 5” or “X is assigned 5”

X

Page 13: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• In programming, we tell the computer the value of a variable• So, for example,

x <- 5;

means “X gets the value 5” or “X is assigned 5”

X

5

Page 14: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• And later we can say something like:

x <- 8;

means “X gets the value 8” or “X is assigned 8”

Page 15: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• If we want to add one to a variable:

x <- x + 1;

means “increment X” or “X is incremented by 1”

X(new)

5X(old)

4+1

Page 16: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• We can create a new variable Y

y <- x;

means “Y gets the value of X” or “Y is assigned the value of X”

Y

X6

6

Page 17: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• We can also say:

y <- x + 1;

means “Y gets the value of x plus 1” or “Y is assigned the value of x plus 1”

Y

X6

7

+1

Page 18: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• All of these variables are integers• They are all whole numbers

Page 19: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• Let’s look at numbers with decimal points:

P <- 3.14159;

means “p gets the value of 3.14159” or “p is assigned the value of 3.14159”

Page 20: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• We should really give this a better name:

Pi <- 3.14159;

means “Pi gets the value of 3.14159” or “Pi is assigned the value of 3.14159”

Page 21: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• We can also have single character variables:

Vitamin <- ‘B’;

means “Vitamin gets the value of B” or “Vitamin is assigned the value of B”

Page 22: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• We can also have single character variables:

RoomNumber <- ‘2’;

means “RoomNumber gets the value of 2” or “RoomNumber is assigned the value of 2”

Page 23: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• We can also have a string of characters:

Pet <- “Dog”;

means “Pet gets the value of Dog” or “Pet is assigned the value of Dog”

Page 24: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Variables

• We also have a special type, called BOOLEAN• It has only two values, TRUE or FALSE

IsWeekend <- FALSE;

means “IsWeekend gets the value of FALSE” or “IsWeekend is assigned the value of FALSE”

Page 25: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Pseudocode

• So let’s say we want to express the following algorithm:– Read in a number and print it out.

Page 26: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Pseudocode

PROGRAM PrintNumber: Read Value; Print Value;END.

Page 27: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Pseudocode

• So let’s say we want to express the following algorithm:– Read in a number and print it out double the number.

Page 28: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Pseudocode

PROGRAM PrintDoubleNumber: Read Value; DoubleValue <- 2*Value; Print DoubleValue;END.

Page 29: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Converting Temperatures

• How do we convert from Celsius to Fahrenheit?

• F = (C * 2) + 30

• So if C=25• F = (25*2)+30 = 50+30 = 80

Page 30: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Converting Temperatures

PROGRAM ConvertFromCelsiusToFahrenheit: Print “Please Input Your Temperature in Celsius:”; Read Temp; Print “That Temperature in Fahrenheit:”; Print (Temp*2) + 30;END.

Page 31: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Converting Temperatures

• How do we convert from Fahrenheit to Celsius?

• C = (F -30) / 2

• So if F=80• F = (80-30)/2 = 50/2 = 25

Page 32: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

Converting Temperatures

PROGRAM ConvertFromFahrenheitToCelsius: Print “Please Input Your Temperature in Fahrenheit:”; Read Temp; Print “That Temperature in Celsius:”; Print (Temp-30)/2;END.

Page 33: Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.

etc.