Online Book Reader

Home Category

Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [7]

By Root 470 0
That is completely normal.

As I write this, my son Otto is six. Otto is baffled several times a day. He is constantly trying to absorb knowledge that doesn’t fit into his existing mental scaffolding. Bafflement happens so frequently, that it doesn’t really bother him. He never stops to wonder, “Why is this so confusing? Should I throw this book away?”

As we get older, we are baffled much less often – not because we know everything, but because we tend to steer away from things that leave us bewildered. For example, reading a book on history is quite pleasant because we get nuggets of knowledge that we can hang from our existing mental scaffolding. This is easy learning.

Learning a new language is an example of difficult learning. You know that there are millions of people who work in that language effortlessly, but it seems incredibly strange and awkward in your mouth. And when people speak it to you, you are often flummoxed.

Learning to program a computer is also difficult learning. You will be baffled from time to time – especially here at the beginning. This is fine. In fact, it’s kind of cool. It is a little like being six again.

Stick with this book; I promise that the bewilderment will cease before you get to the final page.

Part II

How Programming Works


In these next chapters, you will create many programs that demonstrate useful concepts. These command-line programs are nothing that you’ll show off to your friends, but there should be a small thrill of mastery when you run them. You’re moving from computer user to computer programmer.

Your programs in these chapters will be written in C. Note that these chapters are not intended to cover the C language in detail. Quite the opposite: honed from years of teaching, this is the essential subset of what new-to-programming people need to know about programming and programming in C before learning Objective-C programming.

3

Variables and Types


Continuing with the recipe metaphor from the last chapter, sometimes a chef will keep a small blackboard in the kitchen for storing data. For example, when unpacking a turkey, he notices a label that says “14.2 Pounds.” Before he throws the wrapper away, he will scribble “weight = 14.2” on the blackboard. Then, just before he puts the turkey in the oven, he will calculate the cooking time (15 minutes + 15 minutes per pound) by referring to the weight on the blackboard.

Figure 3.1 Keeping track of data with a blackboard

During execution, a program often needs places to store data that will be used later. A place where one piece of data can go is known as a variable. Each variable has a name (like cookingTime) and a type (like a number). In addition, when the program executes, the variable will have a value (like 228.0).

Types


In a program, you create a new variable by declaring its type and name. Here’s an example of a variable declaration:

float weight;

The type of this variable is float, and its name is weight. At this point, the variable doesn’t have a value.

In C, you must declare the type of each variable for two reasons:

The type lets the compiler check your work for you and alert you to possible mistakes or problems. For instance, say you have a variable of a type that holds text. If you ask for its logarithm, the compiler will tell you something like “It doesn’t make any sense to ask for this variable’s logarithm.”

The type tells the compiler how much space in memory (how many bytes) to reserve for that variable.

Here is an overview of the commonly used types. We will return in more detail to each type in later chapters.

short, int, long

These three types are whole numbers; they don’t require a decimal point. A short usually has fewer bytes of storage than a long, and int is in between. Thus, you can store a much larger number in a long than in a short.

float, double

A float is a floating point number – a number that can have a decimal point. In memory, a float is stored as a mantissa and an exponent. For example, 346.2 is represented as

Return Main Page Previous Page Next Page

®Online Book Reader