Tag Info

Hot answers tagged variables

98

Buffy's lovely answer shows well how two variables can refer to the same thing, but since we are in early cognitive development and working with Scratch, in which you can't pass parameters, and can't arrange for two different names to refer to the same instance/spawn, I wouldn't focus my efforts on that point yet. What matters is the idea of a holding place ...

40

My usual explanation is related to names; specifically names of people. People have names. The names are not the people, but can be used to refer to the people. Some people don't have any names at all. This is common just after birth at least, before the parents decide on a name. Names can be changed. A person can have a name for a while and then later ...

27

What about You have alot of values that you need to remember, so you put a label on each one (a little piece of paper to remind you what this value means) so you can find the value easily when you need it.

20

I actually cover this idea in my very first lesson in AP Computer Science, because I have found that students find the different uses of = to be confusing, indeed. I run a unit based on Tom Roger's unit, Java Ain't Algebra, and I also talk about it a little here. I do this right at the beginning because, if mutability is not made explicit, students do not ...

16

When I have taught this, I have said that = in programming is just a different thing to = in maths. It's unfortunate that someone (I say "some idiot", you might not want to use such language) decided to re-use = for assignment in programming, but now we are stuck with it. Programming, while it looks like maths, is more like a series of instructions. x=5 ...

14

The variable that children are most familiar with is score: whether in computer games, sports or board games, a child will almost certainly understand that a score is a value that is clearly not constant. Variables are abstract, but using familiar examples turns this into something easy to grasp. Once numeric variables are understood, you can extend the ...

11

If your students are coming from a math background, teach them that in most programming languages, all variables have a hidden subscript on them corresponding to a time/step-number (i.e. they're functions of time), and something like: $x = x^2$ really means something like: [Let] $x_{t+1} = x_t^2$ You might clarify that such a rule for $x_{t+1}$ only ...

11

Memory is a linear sequence of storage locations. Like a lined sheet of paper. Take a lined sheet of paper and choose a line. In the left margin, write a label. The variable is that label - it gives a name to a spot. Now look for the label and write something to the right of the margin. That is assignment.

10

I think this depends entirely on the programming language you use. Or maybe more accurately, you should choose a language that allows you to introduce the concepts in the order you want. I would use a language called Processing, which is built on top of Java and allows you to create visual, animated programs without any boilerplate code. I'd start with ...

10

I'm self-taught, but here are my thoughts. 0. Language I learned using a combination of Python (once I started to get more serious about programming, I actually don't remember how I stumbled across it) and JavaScript. Consequently, I have good indentation habits, and I haven't had to worry about problems you do have to worry about with some lower level ...

7

When and how should the differences be pointed out to students? When: As soon as you introduce one of those operators... On the "how" - I'd try to not beat around the bush, so to speak, but to point out straight away that only because we use the same symbol, they still have completely different meanings. Are there examples producing unexpected results ...

7

I'm going to explore an orthogonal concept that sheds light on the question at hand: What is a variable? In mathematics a variable is a representative for some value, a name. It may be a name for something we know or don't (abstraction comes in here). In an equation, the variable is sometimes called an "unknown". But that isn't precisely true. An equation ...

7

I just saw Buffy's answer so all credits to @Buffy and though I could simplify it with one word in a way that 9 year old me would have understood. (Although bear in mind I was also mucking around with Visual Basic 6 at 9 years old so this could also be very inaccurate & harder to grasp) A nickname. x = "the name of the program in scratch"; x is the ...

6

I personally teach mutation almost immediately. However, I agree with Buffy that more difficult is not really the metric people are using to decide to teach mutation later. It is about giving certain key habits a chance to grow and develop before you get there. Most people find recursion naturally more difficult than a loop. There is no reason why this ...

6

All of the below is meant to be thoughts/analogies/illustrations to help explain the difference (meant to be said to students) but in reality, this is all dependent on the level of your students. The expression x == x**2 in python is a "test" - is it true, or is it not? Much like when defining a set, you have $\{x|x>2\}$ (i.e., x is only included iff \$x&...

6

In Structure and Interpretation of Computer Programs (SICP) (a book, video series, and university course), they do not cover variable mutation until around half way. They covered what seemed like everything I know before this. Including functions (not procedures) and object orientation. My thought are that these partial sequences makes sense: statements →...

6

It might be a bit indirect but around that age I bootstrapped myself from fill in the blank math problems through basic algebra to more generalized variable use. eg I started with having been given problems like: 5 + __ = 9, what number goes in the blank? From there naming the blank to get to basic algebra problem: 5 + X = 9, solve for X. From ...

6

If I remember correctly, Scratch has two kinds of variables - object-bound and global. When teaching my son about Scratch, at the time of introducing variables I told him he had been working with object variables/properties the whole time: the position x and y (and to a lesser extent the object sprite/appearance (not sure what it is called in the English ...

6

Why complicate matters with contrived analogies like hotel rooms and guests? It is much more straightforward to take the analogy of a simple paper "form" (which absolutely everyone in contact with the Western world has used and understands), and understand that there are spaces or "boxes" on the form which are referred to by names. The ...

5

I think that this is an interesting question, and one the textbooks often ignore or treat as unimportant. I've noticed it because I end up teaching Python, Java and C++ and have seen how my student's mental models lead to misunderstandings in their code. Here are the three things that seem the hardest for my students to understand: In Python, where ...

5

I am not aware that cognitive modeling of memory structures in early programming education has been directly studied, so anything that I say here is entirely speculative. However, I suspect that the models themselves would somewhat depend on teacher presentation, and would otherwise be relatively stable. Take negative indexing as an example. A reasonable ...

5

I don't teach CS in any official capacity but I have tutored a number of friends and colleagues on the basics. I find that differentiating the language I use when describing the different concepts helps a lot, for instance: Mathematical x=x*x = "X equals X squared" Assignment x=x*x = "X is made equal to X squared" Comparison x==x*x= "Test if X is equal to X ...

5

The use of an analogy is to provide a familiar jumping-off point to introduce something new. Since variables are unique features of computing systems, different from how anything else in the universe functions, we need some way to cross the gap. Let's look at how useful the analogy of hotel room and guest is, bearing in mind that most analogies do not ...

4

I've not tried the following, so I'm unable to vouch for their effectiveness, but if you're looking for suggestions: Avoid assigning variables directly (i.e. x = y) Hide the implementation in functions that have semantically clear names (e.g. x.Assign(y) ), although this assumes an object-oriented approach. Something like: x = Int.Create(y) uses assignment ...

4

I am of the school of thinking that starting at an abstract level causes too many students to consider computation as inscrutable magic. And that can lead to broken mental models and buggy ideas about how to code stuff. A 1970's vintage UC Berkeley Intro to Computing course (for non-EECS majors) used a cardboard computer with pencil-and-eraser-mutable ...

4

I'll give a general answer that you need to think about before it really registers. It applies to both the OOP case and the more C-like case. A program with a high percentage of immutable values is easier to reason about and modify. Once something gets a value/state in one part of the program it has that state throughout. So, I'm not sure that "harder ...

4

The concept of a variable is not hard in and of itself, but it is the first conceptual hurdle for students. The problem is that in common usage we use the same symbol for comparison and immutable facts as we use for assignment. By the time we start kids with computer programming they are steeped in algebra where "=" means "is the same as". 1 + 1 = 2 5 - 3 =...

4

The fundamental element of a computer is a memory cell, which we often refer to as a variable. But what is most important about it is that it can instantly take a new value by being given an electrical input. This is the thing to know, and without knowing that, you know nothing about computers. This is the thing that is a unique feature, different from any ...

4

If this child plays Minecraft, you can try the following analogies: A variable is like a chest with a sign next to it. You can put stuff into a chest (assignment). This "stuff" in the chest is the value of the variable. A function is like a workbench, which transforms inputs (like wood) into the result (like wood planks in Minecraft). It is also helpful to ...

Only top voted, non community-wiki answers of a minimum length are eligible