Engineering is Fun!

Now you know how a computer works. The basic concepts are actually rather simple. Of course, actually building a computer is an incredibly complex undertaking. Fortunately, we programmers can focus on programming them.

In the last section we learned that a computer command basically performs these three task:

- Read the instruction and data from memory.
- Perform a calculation.
- Store the result at another address.

Then the computer continues with the next command. Then the next. And so on. This is the programming of the computer.

Each program is just a *sequence* of commands. To think like a computer we have to think in sequences, too! It’s as simple as that. If you can formulate a sequence of commands that delivers the output that you want, then you have designed a program! Writing this down in a programming language is the last step.

Let’s start thinking in sequences with a real-world example: My morning routine. As I can’t do more than one thing at a time (especially early in the morning), this is a good example of a sequence. I start the sequence when the alarm rings. I get up, go to the bathroom and brush my teeth. Then I go to the kitchen and make a cup of tea (no, you don’t have to drink coffee to be a programmer). I eat my breakfast and get dressed. Then I get out of the door. A nice, straightforward sequence. I can also put it in a graphical representation that helps to think about sequences: A flowchart.

Figure 3. A flowchart of my morning routine.

This simple sequence transforms me from the starting state (lying asleep in bed) to the desired state (outside of my house, dressed, fed and clean). It does not read any data yet. As the sequence is the same every day, no input data is needed. But it will be with a little modification: I don’t want to wear the same clothes every day. Instead, I want to wear white socks on odd weekdays and black socks on even weekdays. The sequence now looks like this:

Figure 4. How to choose the socks according to the weekday.

Now there is an input to the sequence: The current weekday. It changes the path of execution depending on its value. It is also called a *condition* or in programming slang an *if statement*. The condition is depicted by the rhombus symbol in the sequence. If this were a computer program, we would read the current weekday from a memory location that contains this information.

But a sequence does not have to be just straight top-to-bottom. We can also move back up in the sequence and perform a step again. This is called a *loop*. Such a loop should always include a check that will finally let the sequence continue. Otherwise our program will never finish.

I need a loop in my morning routine because I keep all my socks in the same box. I have to take a random sock out and check if it has the color that I want to wear that day. If the color matches, I keep it. Otherwise I put it back into the box. Then I take the next sock and perform the same check again. So I have a loop.

Figure 5. How to select two socks of the same color.

I finish the loop when I have two socks with the correct color. But what happens if the box does not contain at least two socks of the color I want? I would stand in my dressing room forever, taking out socks and putting them back. As a human, I would get frustrated after a few minutes and just go with the other color. But a computer does not get angry, bored or hungry. So it will do this into all eternity. You have to give it another exit condition to prevent this case.

Figure 6. How to select two socks if there aren’t enough socks of the same color.

Of course, you could also check that there are at least two socks of the right color in the box before the sequence starts. Whatever solution you choose: you are responsible that the program can finish under all conditions. **Designing the correct sequence that always provides the correct result under all possible circumstances is our job.**

This is the essence of programming: Formulating the solution to a problem in a sequence so a computer can execute it.

Keep practicing this skill with all kinds of problems. Think them through in your head. Draw the solutions on paper. Think about mathematical problems. Think about everyday problems. Thinking them through and writing them down in flowcharts will sharpen your programming skills without needing a computer at all!

If you don’t come up with a problem yourself, I have three problems you can solve:

- Describe your own morning routine. Try to find a twist to it to make the sequence more interesting, like deciding which tea to drink.
- Revisit the sock problem. Create a sequence that sorts the socks according to their color, so you don’t have to do it again every morning.
- Let’s make the sock problem more interesting: Imagine all socks have a unique number. Every day, you want to wear socks that have higher values than on the day before. This means you have to sort the socks according to their number. There are multiple ways to solve this. Try to see how many you can find. Then try to compare them: How many steps does each solution take to sort 10 socks that are in random order.

In the next sections, you will learn about algorithm, functions and data types.

I hope you enjoyed this article. If you have any questions or comments, please contact me.