HTML Blox

Providing the building blox of the web.

  • HTML Blox Home
  • Blog
    • Design
    • Flash
    • General
    • HTML
    • Javascript
    • PHP
    • Programming
    • Web Servers
  • Forums
  • Where to Start?
  • Lessons
    • CSS
    • Design
    • HTML
    • Javascript
    • Programming
  • References
    • HTML
  • Resources
  • About
  • Contact
  • Go Green!

Programming Lessons

  1. Lesson 1: The Basics
  2. Lesson 2: A Brief History of Computers
  3. Lesson 3: The Workings of Computers
  4. Lesson 4: A Brief History of Programming
  5. Lesson 5: High-Level vs. Low-Level
  6. Lesson 6: Compiled vs. Interpreted
  7. Lesson 7: Variables
  8. Lesson 8.1: Arithmetic and Operators - Decimal
  9. Lesson 8.2: Arithmetic and Operators - Integer and Boolean
  10. Lesson 9.1: Conditional Statements - The Basics
  11. Lesson 9.2: Conditional Statements - Branching Conditionals
  12. Lesson 10.1: Loops - The Basics
  13. Lesson 10.2: Types of Loop
  14. Lesson 11: Functions
  15. Lesson 12: Strings
  16. Lesson 13: Procedural vs. Object-Oriented

Programming Reference

  1. No reference yet.

Links

  • W3C HTML 4.01 Strict Compliant
  • W3C CSS 2.1 Compliant
  • Web Standards Group Member
  • Follow us on Twitter
  • Number Overflow - Free Advanced Scripts and Tutorials
  • Azure Ronin Web Design - Professional Web Design and Development
  • XML Sitemap
  • U Comment, I Follow

Feeds

  • RSS 2.0 Feed
  • RDF/RSS 1.0 Feed
  • RSS 0.9 Feed
  • Atom Feed

Lesson 7: Variables

del.icio.us Digg Blinklist reddit

In our previous programming lessons we’ve looked at a lot of history and background theory. Now that you have this background information, we’ll move into the more practical aspects of programming. We’ll begin with one of the most basic ideas in programming, variables.

What is a Variable?

If you are familiar with basic algebra, you are probably already familiar with variables. You’ve probably seen something like this before:

Algebra

Solve for x:
50 = 5*x+10
50-10 = 5*x+10-10
40 = 5*x
40/5 = 5*x/5
8 = x
x = 8

In that example, “x” is our variable. In computers the concept of variables is the same, but we use them a bit differently. Computers can’t really “solve” for a variable like that (though you could write a program to do so), but we use variables to store data which we may want to use or change later. So, on computers, variables are just containers of data.

It’s All In The Name

Another difference from algebraic variables and programming variables is that algebraic variables are typically only a single letter (such as “x”, “y”, etc.). In programming (for most high-level languages), variables can, and usually are, words instead of just letters.

When you name a variable, it is slightly different for each language. Usually languages will have keywords or “reserve words” which are words that have special meaning in the language (such as “if”, “else”, “while”, “this”, etc.). These reserve words cannot be used as variable names.

Also, usually, you cannot begin a variable name with a number. In most languages, an underscore is the only symbol allowed to appear in the variable name and you can’t have spaces.

Please note that these rules aren’t set in stone and each language has it’s own rules. These are just the more common rules.

Most programmers also use what is called a “naming convention” which is simply a style of naming variables so you can easily tell what they are. For example, a lot of programmers will name object-oriented class member data (you’ll learn about this in a future lesson) with a lowercase m at the beginning, and each consequent word begins with a capital letter, so if you made a variable for employee name, the variable would be named “mEmployeeName”.

Then, non-member variable would start the first word with a lowercase letter, and each consequent word would begin with a capital letter. So, if you wanted to have a variable for system wattage, it would be systemWattage.

Naming conventions aren’t requirements created by the programming language, but by the programmer to help them better understand all of the dozens or hundreds of variables a program may have. It is usually good to develop your own naming convention and stick with it, whatever it may be.

These are just examples. There are also sometimes other naming requirements you must be aware of, which are specific to the language. For example, PHP requires all variables to begin with a $.

Here is a list of possible variable names. Try to pick out which ones are generally valid and which ones are not:

Variable Names

aDollar
fiveDollars
5Dollars
box3
ghost_health
grumpy-monkey

In many high-level programming languages, all but 5Dollars and grumpy-monkey would be valid variable names. However, like I’ve said before, each language can have it’s own naming requirements so all or none of those may be valid in any given language.

Variable Types

Some programming languages are “typed”, which means each variable must be a certain type of variable. C++ and Visual Basic are examples of typed programming languages. PHP and Javascript are both typeless programming languages.

In typed programming languages, when you create a variable a specific amount of room (memory) is set aside for that variable, depending on the type of variable. For example, an integer is typically 8-bits (1 byte) long, so 1 byte of memory is reserved for that integer.

Below is a table of common variable types and a short description. Note that these can be different for other languages, and many languages will also have their own unique variable types (or even allow you to create new types).

Variable Type Description
Integer A whole-number (non-decimal)
Float A floating-point decimal number
Character A single ASCII character
String Multiple characters (a string of character)
Boolean A true/false value
Array A collection of data (a group of variables)

There are usually a multitude of additional variable types, but they vary greatly from language to language.

Also take note that even in languages that are typeless, the variable type still matters sometimes. If you try to do arithmetic on two strings, you’ll get some weird values. The only thing that typeless usually means is that variable types aren’t forced, and can usually change (you could use a single variable to store an integer then later use it for a string, which isn’t possible in typed languages).

Both typed and typeless languages have their advantages and disadvantages. An advantage of typed languages is that memory management becomes easier since you know how much memory each variable will need right when it is created, something you don’t know with typeless. An advantage to typeless is that it is sometimes easier to convert values, something which can sometimes be tricky with typed languages.


In this lesson we took a brief look at some concepts for variables. This lesson was brief because the difference between languages when it comes to variables is so great that we don’t want to get too in-depth in our general lesson.

In our next lesson we’ll take a look at some common operators and some basic arithmetic. We’ll also expand our use of variables a bit as well.

This entry was posted on Wednesday, December 3rd, 2008 at 2:06 pm and is filed under Programming Lessons. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

Please Login or Register or fill out the following to leave a reply:

If you haven't posted an approved comment before, your comment will not show up until approved.

  • HTML Blox Home
  • Blog
  • Design Blog
  • Flash Blog
  • General Blog
  • HTML Blog
  • Javascript Blog
  • PHP Blog
  • Programming Blog
  • Web Servers Blog
  • Forums
  • Where to Start?
  • Lessons
  • CSS Lessons
  • Design Lessons
  • HTML Lessons
  • Javascript Lessons
  • Programming Lessons
  • References
  • HTMLReferences
  • About
  • Resources
  • Contact
  • Go Green!
  • Sitemap

Copyright © 2008 HTML Blox

Unless otherwise noted, all images created and copyright HTML Blox.

Operated by Azure Ronin Web Design