What's the best way to introduce variables to new C++ programmers?

Started by
7 comments, last by Dragoncar 18 years ago
I was just thinking that the best way to do it would be to first introduce the concept of literal constants, because most people don't understand that they need explanation. Many new programmers may be inclined to believe that "they are what they are" without understanding that you can't just write char x = a; and expect the compiler to know that a is a literal constant of the character type. A human would understand it, perhaps, but this would probably cause a compile-time error. So, I'm thinking that books should start with that concept and teach the reader all the different ways that literal constants are expressed (with '' for chars, "" for strings, leading 0's and 0x for oct and hex integral constants, and so on). From there, books should move on to symbolic constants, so that the reader can learn how these literal constants can be assigned a label of sorts, and they can be referenced later on in the program using the label instead of the full literal itself. From there, it should be trivial to introduce the idea of variables, i.e. just explain that they are like symbolic constants, except that their values can be changed after the initialization. Of course, anyone who has taken a remedial algebra course will have a basic understanding of what variables are, but there seems to be a few differences between variables in programming and variables in algebra. So, this can get people thinking about how variables, in programming, are just labels attached to a certain address in memory, of a type which determines how many bits are involved and how they should be interpreted, through which these bits can be manipulated. There's my n00bish $0.02.
Advertisement
IMHO it's not the concept of variables that needs to be explained, but the concept of types.
Quote:Original post by uncle_rico
I was just thinking that the best way to do it would be to first introduce the concept of literal constants, because most people don't understand that they need explanation. Many new programmers may be inclined to believe that "they are what they are" without understanding that you can't just write char x = a; and expect the compiler to know that a is a literal constant of the character type. A human would understand it, perhaps, but this would probably cause a compile-time error.

So, I'm thinking that books should start with that concept and teach the reader all the different ways that literal constants are expressed (with '' for chars, "" for strings, leading 0's and 0x for oct and hex integral constants, and so on).

From there, books should move on to symbolic constants, so that the reader can learn how these literal constants can be assigned a label of sorts, and they can be referenced later on in the program using the label instead of the full literal itself.

From there, it should be trivial to introduce the idea of variables, i.e. just explain that they are like symbolic constants, except that their values can be changed after the initialization.

Of course, anyone who has taken a remedial algebra course will have a basic understanding of what variables are, but there seems to be a few differences between variables in programming and variables in algebra. So, this can get people thinking about how variables, in programming, are just labels attached to a certain address in memory, of a type which determines how many bits are involved and how they should be interpreted, through which these bits can be manipulated.

There's my n00bish $0.02.


Not really, because a beginner should start on a higher or lower level language in my opinion. Starting in pascal,php,vb or something will make it easier to explain how to work with chars and stuffies.

The best way to explain symbols is by taking them back to algebra, I remeber someone tried to explain variables to me but failed miserable (i was 12). I ended up understanding it by screwing with the code.

the difference between
char x = a;
and
char x = "a";

Can easily explained by this dilema
char a;
char x = a;
// So what is x now, is it the value stored in a or the letter a
----------------------------

http://djoubert.co.uk
Quote:Original post by Zahlman
IMHO it's not the concept of variables that needs to be explained, but the concept of types.

i agree 100%. variables are as easy to explain as they were in algebra. but the types and how you associate data to them is the most important concept to be learned, assuming you're using C++.

Beginner in Game Development?  Read here. And read here.

 

I was introduced to types over 25 years ago, with an analogy. The teacher had brought in one of those toddler toys with different shaped pegs that could only fit in the proper shaped hole. Maybe it was my 11 year old impressionable mind that helped, but I got the point very easily.
.
The biggest problem I had when teaching my older brother who is far more intelligent in the mathematic department than me (albeit he tried harder *gigglez*) was that you could do:

x = x + 5

Now ofcourse we take that for granted as pain stakingly simple but he couldn't get his head around it and kept saying "x does not equal x plus 5!" so I changed the formula to

y = x + 5

and he understood that obviously. So I changed y to x2 and x to x1 and he understood that too.

x2 = x1 + 5

I had already attempted to explain to him about storing the result back into the same address in the computer and the x was just a reference to that location.

So now I said that this x1 and x2 were just two different instances in time and it sort of started to click. I just think it helps to get them to practice... as you experiment you discover and find the holes and learn the logic.
What we do in life... Echoes in eternity
maybe if you just explained to your brother that x is not equal to x + 5 currently, but that you are changing the value of x by increasing it by 5.
A mathematician would write:

x := x + 5

for an assignment. This is how assignment is written in some languages, but in C/C++, we use '='.
I think the box and warehouse analogy can be good too, because it lets expand it to include types and classes, methods and some other things too.

This topic is closed to new replies.

Advertisement