Objective-C Tutorial 2 – Variables And Data Types
In our last Objective-C tutorial, we covered setting up Xcode, running the Hello World program, and making comments. In this tutorial, we are going to go over variables.
If you went through any basic algebra course, then you probably know what a mathematical variable is. As a quick reminder, a variable is a letter or symbol that represents a value (or values). The value of a variable can change given different equations. In programming, variables are names that are placeholders for a data value. For example, a variable named “x” could represent 5.
There are many different types of variables for different kinds of data values. For example, integer variables can only represent numbers that are integers (integers are pretty much whole numbers like -1, 0, and 1). Floats, or floating point numbers, are variables that represent numbers that can have decimal places (up to 7 digits total). A double is a variable type like a float, but it can hold a much larger amount of digits. When displaying a double in the console window, scientific notation is used for numbers with more than 6 decimal places. Now I know that this paragraph is somewhat confusing and difficult to reference, so here is a comprehensive chart of data values that you can look over now, and back at in the future.
Date Type | Description | Example(s) |
int | variables that are integers can store whole numbers | -1, 0, 1 |
float | variables that are floats can store numbers with (a limited) number of decimals | 3.14, 5.68, 2.333 |
double | variables that are doubles can store a very large amount of decimals and are displayed in scientific notation | 3.141592653589793238462643383279502884197169399375105820974944 |
char | variables that are characters can store a letter | a, b, c |
bool | a variable that is either true or false; boolean values are integers; any number that is not 0 is true, 0 is false | true, false, 0, 1, 2 |
id | id variables can store any type of data as well as entire objects | “hello”, 1, pool |
Don’t worry if you don’t understand some of these data types, we will be going over the use of integers right now, and the others we will cover in the future.
It’s time do dive right in to the code. In this program, we will declare a variable “x” and set it equal to 3. Then, we will output the value of x using NSLog.
#import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int x = 3; NSLog(@"The value of x is %i", x); [pool drain]; return 0; } |
If you run the program (see tutorial 1), you will find that it outputs “The value of x is 3”. Pretty darn amazing, eh? Now lets break it down and go through the lines of code that you haven’t sen before.
int x = 3;
This sets the integer variable x to equal the value 3. It can also be typed as follows:
int x; x = 3; |
NSLog(@”The value of x is %i”, x);
This NSLog statement outputs “The value of x is 3”. In order to output the value of an integer variable (in this case, x) in the NSLog statement, you must place %i in the part of the output (text in parenthesis) where you want the variable to be output. Then, you must follow the output statement in quotes by a comma and the name of the variable that you want the %i to represent (in this case, it was x).
Now, let’s look at some situations that you might run into while using variables. Say you want to output the values of two variables in the same NSLog statement. The code would be as follows:
#import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int x = 3; int y = 12; NSLog(@"The value of x is %i, and the value of y is %i", x, y); [pool drain]; return 0; } |
This will output “The value of x is 3, and the value of y is 12”.
NSLog(@”The value of x is %i, and the value of y is %i”, x, y);
The two %i’s correspond to the variables that are in the same place in order in the list of variables after the quotation.
Another interesting thing you can do with variables is set a variable to equal an equation that can include other variables. The following code demonstrates this:
#import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int x = 3; int y = 12; int z = x + y; NSLog(@"The sum of x and y is %i", z); [pool drain]; return 0; } |
This outputs “The sum of x and y is 15”. The line “int z = x + y;” sets integer variable z to equal the sum of the values of x and y.
The last thing I want to demonstrate is the use of a different data type. Let’s use a float.
#import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; float x = 3.1415; NSLog(@"The value of x is %f", x); [pool drain]; return 0; } |
This outputs “The value of x is 3.141500”. Note how all 6 decimals are displayed, even if you didn’t have that many. Also note that we used %f in our NSLog statement to output a float. For doubles, we use %e. We will discuss the use of other variables in NSLog in future tutorials.
I hope that you found this tutorial helpful! Please feel free to leave comments asking questions regarding this tutorial.
Leave a Comment