2. Using variables

In my previous post, we talked about how there are four different types of variables that make up the core foundation of programming. If you close your eyes, can you recite them? Can you remember what types of data they hold? Review and practice them until you remember them exactly.

Now let's see what these variables look like in action. This is the process of declaring a new variable in Objective-C (the language you need to learn to write iOS apps):

Integer:

int variableNameInteger;

Double:

double variableNameDouble;

Boolean:

BOOL variableNameBool;

String:

NSString * variableNameString;

How does that look? Do you feel okay about that? You're probably wondering why the string type uses a * in the declaration. That is a good question that you don't need to worry about yet :)

Keep in mind that you can set the name to whatever you want, it doesn't matter at all. Now let's go through what we are doing in more detail.

When you declare a variable, you state the type of variable you want to use, and then follow that with the variable name. It is important to remember that a variable has a type, and a name, and those are two very different things.

Also note that we are using ;'s all over the place! This is a programming convention that is basically standard across most computer languages. The ; is used to say that you are done with that line of code. Even though it might seem logical to you that if you move to a new line, that that is a different line of code, the computer doesn't see it that way. You need to tell the computer that that line of code is over with a ;.

So before we can use variables, we need to declare them, or let the computer know which ones we are going to use. So looking over the code that we have up above, do any of our variables have any values? That is the third aspect of a variable, it's value. The purpose of a variable is to be a placeholder of values, right? That means that we can give variables values, and they will retain them.

Pop quiz: What are the three aspects of these basic variable types?

Think about it.

Think some more.

Do you have the answer?

Answer: Ok, a variable has a type, a name, and a value. That makes sense, right?

Lets finish this post up by assigning values to our variables:

Integer:

variableNameInteger = 650;

Double:

variableNameDouble = 345.46;

Boolean:

variableNameBool = YES;

String:

variableNameString = @"This is a string!";


What do you notice that is different about this code? Once you declare a variable, and the computer knows it's type, you don't have to reference that variables type anymore. You can just use the variable's name. So in the code snippets above, we are simply referencing the variable by it's name, and using the = operator we're giving the variable a value.

To give you a better idea of how this works, lets go with one more simple example:

Lets do some stuff with our integer variable after the above line of code that gave it the value of 650.

variableNameInteger = variableNameInteger + 100;

variableNameInteger = variableNameInteger - 4;

variableNameInteger = variableNameInteger * 3;

variableNameInteger = variableNameInteger / 2;

What is the value of variableNameInteger?

Think about it.

Think some more.

Do you have the answer?

Answer: 1119.

Basic math operations are a fundamental part of programming with variables. Variables are quite flexible and very powerful. This is a simple idea and concept, but it is crucial to understand very well before you move forward. Everything you do with programming involves variables and values so make sure you understand these concepts perfectly.

Coming up we will get into the iOS SDK XCode, and look at how we can declare and use variables in a real life situation.

1 comment:

  1. Excellent work on the variables explanation Wes! Now on to xcode!

    ReplyDelete