Why learn computer programming?

There are many reasons why you might want to learn how to write a computer program. For me though, there were two main reasons. First, most off-the-shelf computer programs almost never do what you need them to do. They might do eighty percent of what you want, or they might do what you want but not in the way that suits you. The solution to this problem is to write the computer program yourself. That way, you get exactly what you want. Second, programming is a creative process. After you’ve finished writing an app you’ve got something, something you’ve made, something that you can show people and say “yes, I made that”. There are other benefits too. Programming teaches you to think logically, teaches you to solve problems, and teaches you to keep going.

How I learnt.

There is no ‘right’ way to learn a programming language. But here’s how I did it. I learnt in two stages. Stage one, which I would call the ‘pre-programming’ phase, started in 2003. I wanted to find a way that I could extend the usefulness of Excel. I learnt that there was a scripting language that was built in to Excel that allowed you to create small programs that would interact with Excel. The language was Visual Basic for Applications. I didn’t get that far with it though, mostly because VBA wasn’t available for the Mac. From there I decided that I wanted to create my own website, so I started learning HyperText Markup Language (HTML) and Cascading Style Sheets (CSS). HTML creates the structure of a webpage and CSS defines what the webpage looks like. For example, the structure of a basic webpage is this:

<title>My Fantastic Webpage Title</title>
#paragraph1 p { font-weight: bold; }
#paragraph2 p { text-decoration: underline; }
<div id="paragraph1"><p>This is paragraph one.</p></div>
<div id="paragraph2"><p>This is paragraph two.</p></div>

So that’s how to define the structure of the page. You’ll see that each ‘tag’ is inclosed in a set of < > brackets. These tags define regions of the page, like the <head> and <body>, but also where actual content is displayed like the <p> tag. But what if I want the text in the first paragraph to be bold and the text in the second paragraph to be underlined? This is what CSS is for. You’ll notice that I’ve used two tags called ‘div’, which stands for division. These are used to contain a block of HTML and therefore separate it from another block. These two divs are given unique names to identify each one. The CSS to achieve the bold and underline is shown between the two <style> tags. Normally you would place the CSS in a separate file rather than at the top of each webpage, but I’ve done that here just to show you how it works.

This is displayed on the webpage like this:

My Fantastic Webpage Title

This is paragraph one.

This is paragraph two.

I really enjoyed learning HTML and CSS. I include this phase in how I learnt to program because learning HTML and CSS gave me confidence. Confidence that I could learn to understand and use a language that is made up of strange looking symbols. Then, Apple launched the iPhone, and a year later the iPhone SDK (Software Development Kit) that allowed 3rd party developers to write apps for the device. As an iPhone user myself, I quickly realised that a typical iPhone app wasn’t as complicated as an app you’d find on a desktop computer. This meant that the path to learning how to program might be a little easier than writing a full-blown desktop application. So I started.

iOS apps are typically written in Objective-C, although you can use other languages like C++. Objective-C is built on top of C, a language that’s been around for a long time (since the late 1960s). An example of some Objective-C is shown in the image below:


You can see a number of things in the image above. On the left is the user interface (UI), which is the stuff you see on the screen. On the right is the actual code that is triggered when someone interacts with the UI, such as tapping on a button. In the example above there are two text fields for entering text and two buttons (Change Text and Clear). The function of the app is to calculate the sum of the two numbers entered into the two text fields. The sum is calculated when the user taps on ‘Change Text’. The result of that calculation is then displayed in the label at the top of the screen.

Here is the first line of code (line 100 in the image):

- (IBAction)changeText:(id)sender
IBAction indicates that this method is hooked up to something on the screen, which in this case is a button (the Change Text button). So when the user taps on the Change Text button this method will be triggered and all of the code within the curly brackets { } will be executed. Next, the name of the method (changeText) is displayed followed by an ‘argument’. An argument is a piece of information that can be passed into the method. In this case the argument is called ‘sender’ which is just a temporary name to indicate the object that is triggering the method.

Here is the next line of code (line 101 in the image):

int num1 = self.number1.text.intValue;
On the first line of the actual code that gets executed (line 101) we define an integer variable called ‘num1’. Integers are whole numbers, so this variable will store any whole number that we assign to it. You’ll notice to the right of num1 is an equal sign. That indicates that we want to put a value into num1. The value we want to assign to num1 is ‘self.number1.text.intValue’. This looks complicated but it’s not really. You’ll see in the image above that ‘number1’ is in green. This indicates that it’s an object of some sort, and in this case that object is the text field on the left (with the number 2 in it). Next we ask for the ‘text’ attribute (called a property) because we want to access the text that is inside the text field. One problem that we now face is that the text inside the text field is text, but we want to put that value inside num1, which is expecting an integer, which is a number. If we try to put text into a variable that is expecting a number then we will get an error. The solution is very simple because text fields have a property called ‘intValue’ which converts text into an integer. The next line (line 102) does exactly the same only this time for the second text field.

Here is the next line of code (line 103 in the image):

self.myLabel.text = [NSString stringWithFormat:@"The sum of number 1 and number 2 = %i", num1 + num2];
On the third line of the method (line 103) we then do the calculation and put the result into the label called ‘myLabel’. NSString is a ‘class’ that defines all sorts of methods for manipulating strings (characters and words). One of the methods within the NSString class is ‘stringWithFormat’ which allows you to combine words and numbers together into a single string that you can then use in a label. You’ll see at the end of the red coloured text ‘%i’. The %i indicates where the result of the num1 + num2 calculation will be placed within the string of words. The symbol ‘i’ is used because we are dealing with an integer. Once the stringWithFormat method is finished it will return (give us back) a string that says ‘The sum of number 1 and number 2 = 5’, which we can then assign to the text property of our label.

If you would like to try some programming in Objective-C you can download the Xcode project file below as a zipped file (download it from your desktop computer as it won’t open on an iPhone/iPad etc). Xcode is the desktop application that is used to write iPhone, iPad and Mac applications. To open and run the project you’ll need a Mac running OS X Lion, which has the Mac AppStore built in. The reason for this is that Xcode can be downloaded for free from the Mac AppStore. Without Xcode you won’t be able to open the project file and Xcode only runs on a Mac (there’s no Windows version, although you can use a Mac over the web from any computer using MacinCloud).

HelloWorld Xcode Project

Once it’s been downloaded and been unzipped you’ll see a folder that contains a file called ‘HelloWorld.xcodeproj’ and another folder called ‘HelloWorld’. If you have Xcode installed you can double-click on the xcodeproj file to launch the project in Xcode. When the project opens in Xcode you’ll see a list of files in the left-hand column. Find the file called ‘MainViewController.m’ and select it once. The file will open in the main window. Scroll down to find the method called ‘changeText’. Here you can play around with a few things, like changing the + symbol to a -, or a *, or a /, which are the symbols for minus, multiply and divide. You could also change the text inside the double quotes (" ") and see what happens (don’t change the ‘%i’ though).

Well, that’s just a very brief introduction to Objective-C, but it should give you an idea of what it’s like to use the language. If you’d like to explore the language in more detail and start writing your own iOS apps then scroll down the page a little for the section called Resources.

There are two ways of learning Objective-C. Some say you should learn C first and then learn Objective-C. Others say that you can jump straight in and learn Objective-C. I started learning C first. However, I quickly realised that much of the learning did not include programs that used a graphical user interface like you find on the iPhone. So I decided to jump straight in and learn Objective-C. Looking back from my experience I would encourage anyone wanting to learn iOS programming to bypass learning C and start straight away with Objective-C. You can pick up the basics of C while also learning Objective-C.


So where can you get help with learning? There are three main sources: (1) books (2) videos (3) forums. The three main books I have used are Programming in Objective-C, Sams Teach Youself iOS 6 Application Development and Beginning iOS 6 Development. Video resources are abundant on YouTube, including my own series. Finally discussion forums are a great way to get help from other developers when you get stuck. Apple have their own forums but you need to be a registered developer to gain access. Otherwise, StackOverflow is probably the most popular.