Here are the scratch code files, in case you want to follow along.
Programming 101 Let’s get started! Martin (馬田) S. Stoller 20161016 The first computer I programmed... MOS 6502 CPU @ blazing 1 MHZ! 40x25 character green screen! 4 (four!) KB of RAM! Datasette Tape Storage! Solid full metal construction! You don’t have to be Spock to learn how to program a computer! Variables The core of any program. Well, almost any... There exists a set of programming languages that do not use variables. These are called functional languages. They do have data items, though - they just are not “variable” - ie: mutable, ie: changeable. There also exists a set of esoteric programming languages that uses _NO_ data. These languages are known to make even the most hardened coders cry in frustration. Not to brag (too much), but I’ve written working programs in several of these esoteric languages. Variables ~= Bucket You can think of a variable as a bucket in the computer’s memory that can temporarily hold anything you need to put into it. This is a good way to visualize what a variable is. A variable can hold a single letter, a number, a piece of text, even an image or movie. ~= means “almost equal”. To allow us to find our variable in the computer memory (ie to reference it), we give it a unique name. I like to think of the name as either an index card or a street address. In this case we have marked our bucket (symbolizing our variable) with the name “A”. PS: Variables (and our bucket) are an abstraction of the data we are storing in computer memory. Variables have names R.A.M. All our variables are stored by the computer in its memory, also called RAM (short for Random Access Memory). The other type of memory available to most computers in the past was SAM, or Sequential Access Memory - typically available in the form of magnetic tapes. We won’t bother mentioning ROM - Read Only Memory - at this point. ROM is boring, these days. Scratch - Variables In SCRATCH you create a variable by clicking on DATA, then choose the button “Make a Variable”. For now we’ll make a general purpose variable called “A”. In SCRATCH that would be “For all sprites”. Remember our bucket called “A”, which could hold anything? Same as our bucket, in SCRATCH our variable “A” can have anything stored in it. In SCRATCH all new variables start off with the defined value of ZERO (0) -> Scratch - Variables To give our “A” variable a new value, say ten (10), we use the command “set A to 10” which looks like this -> We could also have put the word “Ten” into our “A” variable -> I like to visualize that as if I’m putting the number 10 into our “A” bucket. PS: We could have name “A” differently. “Brunhilda”, for instance. Scratch - Setting a variable’s value 10 Here is our first program. We need to add “When flag clicked” from the “Events” menu, so the program knows when to start. When we run the program, we’ll see our “A” variable is set to ten -> We also added “when space key pressed” so we can actually interact with the program. With the program running, if we hit SPACE, our variable changes to -> First Program - DEMO TIME! Variables are very handy. But by themselves, they are sort of boring. That’s where operators come into play! Hint: we already saw a sneaky operator at work on the previous slide. Did you spot it? OPERATORS Operators In short, operators operate on data. Some operators you know from everyday life are the math symbols, such as the plus for addition ( + ), the minus for subtraction ( - ), the X or asterisk for multiplication (x or *) and and the slash or colon for division ( / or : ). There are also more complex operators, for example to compare values, or evaluate logical statements, set values, or simply to enforce precedence. We’ll look closer at some of these. Main SCRATCH operators Here are the main operators that SCRATCH supports. The first four are our basic math operators, +, -, * and /. The last three are called conditional operators, less than, equal to, and greater than. They return a TRUE or FALSE value, depending on the condition’s validity. More on those a bit later. SCRATCH operators demo Yep, “set” is also an operator. Control Structures We have our variables. We have our operators. Now we need to make them work for us! Control Structures Control structures in our program allow us to make decisions or repeat pieces of our program several times. These are the two types we will look at today. In some programming languages there are also control structures that allow you to jump around in the your program. The use of “jump” structures is frowned on in modern languages, as their misuse can easily introduce unwanted bugs or side effects. Funnily enough, machine language - which every program boils down to - would not work without the ability to “jump” around... SCRATCH - Control - IF...THEN In SCRATCH we can use the “IF...THEN” control structure. With the help of a conditional operator (the = in this case), we can check if the variable “A” has the same value as the variable “B”. If it does, we can run the function “say” to print a sentence for us. A function, btw, is sort of a small program that has already been written for us, that we can call up to do predefined tasks. SCRATCH - Control - IF...THEN And here we’ve put it all together. Probably makes a bit more sense now, too! And we can even read our mini-program as if it was English -> If variable “A” is equal to variable “B”, then say “Yes, A is equal to B” for 5 seconds. Which is what the cat will do for us. SCRATCH - Control - IF...THEN...ELSE Now sometimes we want to do something different when our IF statement ends up being untrue. That is where the IF...THEN...ELSE control structure comes into play. In this case, if “A” was 20, and “B” was 30, the SCRATCH cat would end up saying “NO! A is NOT equal to B!”. Next up, the full “IF” example... SCRATCH - Demo Time! IF...THEN...ELSE We mentioned that there were control structures that help us repeat pieces of our program several times. These are also called “loops” - because our code is looped! In SCRATCH that would be the “Repeat Until…” control structure. Without loops, we would have to copy the code we want to repeat as many times as we want it to run… which would be a lot of work, and maybe even impossible! SCRATCH - Control - Repeat Until... (aka “While...”) SCRATCH - Demo Time! Repeat Until... SCRATCH - Forever. The control structure “Forever” is a simplified version of “Repeat Until...” - in essence it is a shortcut for “repeat until infinity”. Your computer’s operating system is a classic example of using a forever loop. Your computer will run indefinitely - until it dies of a heat death or you mercifully turn it off. Most apps also run on a forever loop, and run endlessly, or until you shut the app down. Putting it all together. We’ve already had a couple of simple programs. Here is an example of a more complex program, using all of the above learned items! This is the code that runs the SCRATCH cat sprite. A sprite is computer talk for a piece of computer graphic that is usually animated on the screen. SCRATCH - Full Blown Demo. SCRATCH - Full Blown Demo. This is the code that runs the SCRATCH basketball sprite. Fun fact: Your mouse pointer is actually also a sprite! And here’s a YouTube video of what the above code does! Bonus Material We are done with the main presentation. But: In case you ever read this slide-set again after the live presentation, here some bonus information on programming! A note on Variable Names Most programming languages have distinct rules on how we can name our variables. Almost ubiquitously, spaces, newlines, and special non-printable characters in variable names are forbidden. A good practice is to always start a variable name with a letter. Numbers and in some cases the underscore “_” are allowed, so you could use them as extra information. CamelCase is also a great way to make very readable variable names. Next, some example names, good and not so good. Variable Name Examples Good examples: firstPage gameScreenX Player1 Player2 playerTwo Player1_Score playerScore2 Bad or forbidden examples: 123 Razor Fast Sam! Whazzup? Dude! Lives@Street #3pretzel Party!On!Dude! Remember our bucket called “A”, which could hold anything? Many programming languages force variables to be of a specific type, either text, numeric, or more complex, very specifically defined types (often called data structures). This is called TYPING the variable. Typing variables has its pros and cons - we won’t go into that here, as whole libraries have been written on the subject. Other programming languages allow anything to be stored in a variable - just like our friendly bucket here. A note on variable types SCRATCH - Events SCRATCH supports events. Events allow you to catch input from the user during your program execution. Or to catch messages being passed to your program from other programs. You can then create code to react to (also known as to handle) the event - or even simply ignore it, as the case may be. This slide template is by the great peeps at: If you need a slide template for your own work, check them out! Their templates are free! Credits http://www.slidescarnival.com/ Thanks! If you liked this - or not - let me know in the survey here: https://goo.gl/forms/HlpTjHeNhRVi5aRe2 Cheers and thanks again for watching! - Martin S. Stoller.