cre programming club - class 2 robert eckstein and robert heard

17
CRE Programming Club - Class 2 Robert Eckstein and Robert Heard

Upload: ronald-freeman

Post on 21-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CRE Programming Club - Class 2 Robert Eckstein and Robert Heard

CRE Programming Club - Class 2

Robert Eckstein and Robert Heard

Page 2: CRE Programming Club - Class 2 Robert Eckstein and Robert Heard

What Are Variables Again?

Variables vary (change), and constants are constantly the same.

In the world of programming, there are values in your program that need to change all the time, and there are values that will stay the same. For the most part in Small Basic, you’ll be using variables.

Page 3: CRE Programming Club - Class 2 Robert Eckstein and Robert Heard

VariablesHow do we create a variable in Small Basic? Well, we just give it a name and set it equal to something. Most variables names are short words or single letters.

A = 20

This is kind of like getting a box, writing “A” on the outside, and putting the number 20 inside of it. If you come along later and say, “Hey, I’d like to use what’s in A, you’re going to get 20.” At this point in the program, A = 20.

Page 4: CRE Programming Club - Class 2 Robert Eckstein and Robert Heard

Variables

Let’s test that out by adding the following line afterwards:

TextWindow.WriteLine(“A is “ + A)

This command will write out the current value of A. What happens if we put the following after it?

A = 21

TextWindow.WriteLine(“A is “ + A)

Page 5: CRE Programming Club - Class 2 Robert Eckstein and Robert Heard
Page 6: CRE Programming Club - Class 2 Robert Eckstein and Robert Heard
Page 7: CRE Programming Club - Class 2 Robert Eckstein and Robert Heard

What Can a Variable Store?

So what can a variable store? Well, with Small Basic, one of two things.

We already know it can store a number. For those of you working with fractions and decimals, variables can store any number, including those with decimals. So, 40 and a half would be written as 40.5.

Page 8: CRE Programming Club - Class 2 Robert Eckstein and Robert Heard

Variables Can Also Store Words!

Hmmm… how do computers handle words? Well, kind of like what you did as a baby with building blocks. Remember that computers are dumb and they really, really love codes.

Turns out the computer has a code number for each letter, a code number for a space, a code number for a questions mark, and, well… a code number for pretty much everything on your keyboard.

Page 9: CRE Programming Club - Class 2 Robert Eckstein and Robert Heard

What Can a Variable Store?

When it wants to use words, it just strings the letters and symbols together like the building blocks of a baby. And, when it’s done, it adds on a special code (usually 0) that says, “Okay, this is the end of the it!”

So, we know about numbers, but what are these words or sets of words or symbols or whatever called?

Page 10: CRE Programming Club - Class 2 Robert Eckstein and Robert Heard

Strings Must Have Quotes!

Let’s write a string and assign it to a variable.

A = “Hello There!”

Note that a string is always surrounded by double quotes. What happens if we write?

A = Hello

Small Basic will think we want to take the value in the variable Hello and copy it into A.

Page 11: CRE Programming Club - Class 2 Robert Eckstein and Robert Heard

Try Some of These...A = “Hello There”

A = 12 + 45

A = 1 + 9 * 4

A = B

A = A + 1

A = (B * C) / D

A = Math.PI

A = Math.PI * R * R

A = currentArea - 1

A = “Hello “ + “There”

A = “12” + “34”

A = “Hello “ + 12

Set the other variables (B, C, D, currentArea) to something if you need to! Can you set Math.PI to

something?

Page 12: CRE Programming Club - Class 2 Robert Eckstein and Robert Heard

Adding Strings and Numbers

What happens if we add two numbers together? What happens if we add a number to a String? Experiment and see what happens!

A = “Hello”

A = A + “ There ”

A = A + 12

Page 13: CRE Programming Club - Class 2 Robert Eckstein and Robert Heard

Point of ExecutionThis tells the exact point where the program is executing.

A program will execute line by line. When it finishes one line (a statement), it moves onto the next. And the next. And the next.

And it keeps going, unless you tell it to go somewhere else in the program. So if I tell it later that A = 21, then 20 gets thrown away, and A now equals 21.

Page 14: CRE Programming Club - Class 2 Robert Eckstein and Robert Heard

No Confusion

So, the important thing to remember here is: a program won’t do what it’s told to do UNTIL IT GETS to that line, and when it gets done executing that line, THE COMPUTER HAS DONE IT.

Page 15: CRE Programming Club - Class 2 Robert Eckstein and Robert Heard

Computers are Dumb

Like they said in the video last week, deep down computers are pretty dumb. But they’re actually a little like football quarterbacks.

Have you ever seen a football quarterback come up to the line and say something like “43! 21!”. Each of those numbers is a secret code that means something to the other players.

Page 16: CRE Programming Club - Class 2 Robert Eckstein and Robert Heard

Computers are Dumb

Computers are the same way. They get a number like 43, and that tells them do so something. And then they get a number like 21, and that’s a code too. And they know what to do with that number too! Each code number means something, and it never changes. This is called machine language.

Page 17: CRE Programming Club - Class 2 Robert Eckstein and Robert Heard

Next Time....

Keep experimenting with variables between this class and the next! Try things at home!

Next time, we’re going to learn some more about how computers work and some of the branching functionality that is available.