|
What in Bill Gate's name is a variable?
A variable is just a way of reserving space inside of the computer. You are basically saying, "Hey you! Reserve some space would ya?" Why would you want to reserve space? So you can take different numbers and add them or hold a string of words and print them out over and over without typing in the sentence over and over. In C++, we break down the types of variables into data-types. There are numerical, character and Boolean data-types. The numerical datatypes are as follows:
Int depends on if you have a 16-bit or a 32 bit compiler. A 16-bit int is the same as a short while a 32-bit int is the same as a long. Its weird only if you are new to the world of compilers. Moving on. The char data-types are:
Chars (pronounced char as in char-broil in Texas and 'care' as in character in other places) are used for holding letters and words. How? One word: ASCII. I will talk about chars and ASCII, later in Lesson Eleven. There is only one Boolean data-type: Bool. Each bool uses 1 byte and can be either true or false.
Now, you are probably going," How am I suppose to use these data types?" Easy! First declare them inside of your main (There are other places to declare it but not now). Then define them by giving them a value. We define a variable's value by using the assignment symbol, = . There are some rules to this assigning variables. First, the variable getting a value is on the left. There can only be one variable to the left of the assignment symbol. You can not give a constant a value. And you can not have a constant on the left. 5 = 17 is a no-no. x = 17 is good. An example of all of this:
Don't worry. We will review ALL of the stuff we just went over now. But if you need more help then send me a line:
Next page: |