Variables & Constants - FREE Guru Level Training For Beginners (2014)

FREE Guru Level Training For Beginners (2014)

Chapter 7: Variables & Constants

“On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question.”- Charles Babbage

In this chapter, you will learn about an overview of objective C basics for app development, including:

· Constants

· How to change the variable’s behaviour

Variables are used to stare data by an app. These are certain locations in the computer memory and every variable is given a name and a value is assigned to these too.

You can use a float, double, integer or character when assigning a value to your variable. Also, the value of the variable cannot be changed. You cannot change the variable type but it is possible for you to assign it a different value. This can be done after declaring and initializing the variable.

To use a variable you must first declare it. Here’s an example of how you can do this:

int myAge;

Now, using an operator I will assign the variable, myAge, a value:

int myAge = 25;

In this example, I have declared the variable and initialized it by assigning it a value of 25.

You may give the variable another value after you have initialized it.


Once you assign a value to a constant, it cannot be changed even after it is initialized (thus the term ‘constant’). Also, in Objective C it is necessary that you declare and initialize the constant at the same time. Look at the following example:

const int myAge = 25;

You have to insert the const prefix to tell the programming language that you have inserted a constant. If you try to change the value of the constant, you will get an ‘error’ and the code cannot be compiled. Let’s see how this works:

const int myAge;

myAge = 25; // the attempt to initialize the constant after declaration has failed.

How to Change the Variable’s Behavior

While changing the variable’s type is not possible, you can certainly make the variable act/behave like another type of variable. This process is called type casting.

Bear in mind that the variable will only behave like another type of variable once. Let’s see how type casting works:

double speed = 20.4;

double speedRate = 5.78;

double result = 0;

result = speed * speedRate;

NSLog(@"The result is %x", result);

Once this is executed you will get the following answer:

The result is 117.912

Let’s say you want to round this off so that the result is equal to the nearest possible integer:

double speed = 20.4;

double speedRate = 5.78;

double result;

result = (int) balance * (int) interestRate;

NSLog(@"The result is %x", result);

By inserting int in the result we can change the behavior of the variable, double, so that it behaves like int. Upon compilation you will get the following:

The result is 110.


This chapter teaches you about variables and constants. Remember that you cannot change the variable type but you can change how a variable behaves.