Mastering macOS Programming
上QQ阅读APP看书,第一时间看更新

Imperative

Let's start with what you know already, assuming that you have some background in Swift, Objective C, Java, or one of the many other C-based object-oriented languages.

The word imperative comes from the Latin word for command. And that is exactly what imperative programming does; it defines code step by step, with an explicit order of execution. Imperative programming means taking responsibility for everything that needs to be done; imagine sitting next to the driver of a taxi, giving instructions to get to some destination:

turn left 
drive two hundred meters
stop if the traffic light is red
turn right if it is green
...etc.

Imperative programming often uses subroutines, so in the preceding example, turn left would represent a block of instructions somewhere else that would read as follows:

move the steering wheel anticlockwise 
when the car is moving parallel to the pavement, return the steering wheel to its default position

And so on.

Another characteristic of imperative programming is the use of iteration:

Put a suitcase into the trunk of the car 
If there's still room in the trunk, go back one line.

The use of such loops is a mainstay of imperative programming.