I'm going to do a 2-3 part tutorial about object-oriented programming with Objective-C. This first part is about what is basic programming and what is an object.
To those of you who have never programmed before, essentially the job of a programmer is to build and debug computer programs. We do this by using programming languages. There are two types:
- Low-Level - This is machine code(0's and 1's) / assembly language(more readable 0's and 1's, but not by much). To explain what happens on this level, the program will tell the board to move electricity around the board. Don't ask me to go into depth(don't fully understand low-level), but this is the direct way to talk to the computer, and you have to know a lot about the machine your programming on.
- High-Level - This is the level you will program on (unless you do operating systems/computer engineering). Some languages are C, C++, Java, etc. On this level, we have to compile the code we type (I'll go into compiling in a debug tutorial) which will turn the code into something the computer can read. NOTE: HTML, XML, XHTML, etc. are not high-level, they are markup.
Common Programming Across High-Level Languages
There are 5 common data types across computing that we normally assign variables to
- Integer - int - A numeric value that does not have a decimal point. Often used for counters. Can become negative. Example: 2
- Real - float/double - A numeric value that has a decimal point. The reason for a second type of numeric value is because we can't store a real number like an integer in binary (google floating point). Some languages will convert 2 to 2.0 or vice versa. Example: 3.34
- Character - A single character. Simply an integer that follows some sort of text encoding such as ASCII or UTF. Use ' around the character you want to store into variable. Example: 'c'
- String - A group of characters that are "stringed" together. Many strings have functions to determine length, character at a certain index, etc. Becauses computers don't understand what white space is(enter key), you use a line-break character (normally \n) to go to a new line. Uses many bytes in creation. Use " around string you want to store into variable. Example: "This is a string."
- Boolean - Represents True(1) or False(0). Named after an important math (Boolean algebra) used in computer science. This is used to determine if something is equal to something else. Go to Boolean section for more. Example: 1 == 1 (means True) | 1 == 0 (means False)
- Null/Void - Represents nothing. Not really a data type, but basically if we want to represent nothing, you use null/void. It's not really 0, more like non-existant. Numbers will normally represent null as 0 and a string/char is empty ("" or '').
- Array - Not a data type. Represents a group of the same data type. Arrays start at 0 not 1 and end at n-1 where n is the number of items in the array. For example int x creates an integer array with 10 spots for integers and the last integer is held in x. NOTE: I'll go more into NSArrays next tutorial.
This is the normal math operation used across multiple languages. (assume all variables are int)
- Equals - = - means to set a variable to something on the other side of the equals sign.
x = 2 //sets x to 2
- Addition - + - adds two numeric values.
x = x + 2 //sets x to x plus 2. the x on the right side of the = is the value of x before it is set
- Subtraction - - - subtracts two numeric values.
x = 4 - 2 //sets x equal to 4 minus 2
- Multiplication - * - multiplies two numeric values.
x = 2 * 4 //sets x equal to 2 times 4
- Division - / - divides two numeric values. You have to specify if you want the number to be a real number. By default, dividing causes the number to be integer that has been truncated (always rounded down). Never divide by 0(crashes program if used).
x = 5 / 2 //sets x to 5 divide 2. x will equal 2 unless you specify you want a decimal, then it will be 2.5
- Modulo/Remainder - % - Gives the remainder of divide. The number on the left of the percent(dividend) is divided by the number on the right of the percent(divisor). The value of the remainder is never higher than the divisor - 1. 0 indicates the divisor divides perfectly into the dividend. Will never tell how many times it divided into dividend.
x = 5 / 2 //sets x to the remainder after 5 is divided by 2. x equals 1.
- Power - ^ - Multiplies a number by itself by a certain number of times.
x = 3 ^ 3 //sets x equal to 3 times 3 times 3.
Boolean is used to determine if something is equal to another thing. Here are some normal operations used across multiple languages. (assume all variables are int)
- Is Equal To - == - Evaluates True if the variable/equation/number on the left is equal to the variable/equation/number on the right, if not then False.
x == 2 //if x equals 2 then the statement is true
- Greater Than(or Equal to) - > | >= - If the value on the left is more/greater than the value on the right, evaluates True, otherwise False. If using >=, same rules apply, but if the value on the left is equal to the value on the right, it will evaluate True instead of false.
x > 2 //if x equals 3 or more, the statement is true
- Less than(or Equal to) - < | <= - Same idea as Greater than, but value on the left has to be less (or equal to if >= is used).
x < 2 //if x equals 1 or less, the statement is true
- Not - != | ! - If the value on the left does not equal the value on the left, evaluates True, otherwise False. A ! can be used before a variable/equation to make it false.
x != 2 //if x does not equal 2, the statement is true
- And - && - If the statement on the left and on the right both evaluate true, then the And statement is true, otherwise false.
x >= 10 && x <= 20 //if x is between 10 and 20, then the statement is true
- Or - || -If the statement on the left or on the right evaluate true, then the Or statement is true. Both must be false for the statement to be false.
x != 6 Or x <= 6 //if x is less than or equal to 6 or does not equal 6, the statement is true
Function/method - (varies) - A block of code that is read from the top to the bottom. You normally have to specific what datatype it will return(or void if none), give it a name that is unique from other functions/methods, and if any data needs to be passed through and what that data's datatype is.
//do stuff to string
If Statement - A block of code that is performed if the Boolean statement is true. Else can also be used with the if statement to make another block of code be performed if the Boolean statement is False. Else If can be added to check for multiple scenarios.
if (x == 1)
else if (x == 2)
//do something else
//do something different
Switch Statement - Runs a variable through different cases that are true. Look at example for more info
//if x equals 1 it will perform this block
case >= 1:
//if x equals 1 or is greater it will perform this block
break; //exits the switch statement, without this, your program will run through every statement that is true.
//runs no matter what x equals
Do/While - A block of code that repeats itself while the boolean statement is true. Infinite loop if the statement never becomes false (1==1).
while (x < 10)
For - A block of code that repeats itself while the boolean statement is True. Infinite loop if the statement never becomes false(1==1).
for(int x = 0; x < 0; x++)//left used for variable declaration(optional), middle is the boolean statement(required); right is what we add to variable after loop completes(optional)
Objects? What are they?
Now we're going to get a little bit into objects and the next tutorial I'll go into depth about Objective-C. An object is an entity that is manipulated with computer code. I guess I should go back a bit in history to explain this. Before some of us were programming(about the 50's/early 60's), computers were getting more complex and people wanted more powerful programs, but in non-object-orientated programming, it was hard to control our variables because the programs consisted of many lines(think Angry Birds' source code length * 10) and variables were more global. Objects allowed us to control access and focus more on the data than processes.
Objects belong to some sort of class. The class acts as a template for the object and allows for us to identify the object's methods and properties.
The main problem we worry about with objects is memory. All objects require RAM to exist, and the more you make the more RAM you use. Essentially this is the life of an object on the computer's level:
- Determine how much RAM the Object will probably take
- Allocate part of the RAM to the Object(if it can't, it could remove other objects or not alloc your object)
- Connect methods for the object
- Do super classes initialize(if any)
- Do class initialize
- Do something with object until deletion
- When release is called, set data on RAM to NULL
- If deallocating, object loses it's spot on RAM and memory it took up goes back to Free RAM Space
Next tutorial will be about Objective-C and more on what objects can do.