Let's talk about one of the most important types of abstractions, state. Any computer program can be in a number of different states, and can change states. What is a state? It's a temporary configuration of a computer system that affects how it responds to interaction. That sounds very technical. What does it mean in practice? Let's start with the basics. Any computer has two states, it's on, or it's off. In each state, it will respond to interaction in different ways. If it's on, it does quite a lot. If it's off, it doesn't do anything. If I shut the computer down, it goes from on to off. This simple example captures the idea of computer state. At any given time, a computer is in one of several states. That state affects what the computer does, the computer changes between states due to user input, or possibly, through its own internal decisions. What about the video player that we talked about earlier? What states do you think a video player has? When you first load up a video player, it is normally in a stopped state. The playhead is at the first frame, and the video isn't moving forward. If you press the play button, it enters the playing state, where the playhead is constantly moving forward. At anytime, you can hit pause, to enter a pause state, where the playhead is somewhere in the middle of the video, but not moving forward. You can always enter the playing state again by pressing play. When the video reaches the end, it will return to the stop state. If you get into the details, are often other states: for example, for online video, the play will actually start in a buffering state, which is where the video is downloading, and you have not loaded or buffered enough data to stop playing video, which only enters the playing state when enough data has loaded. What's interesting, is that the player is switching between playing and paused when you click a button, through user input. But the switch between playing and stopped, happens automatically at the end of the video, without user input. So, states can change because of both internal and external factors. In fact, an online video will often enter the paused state when the internet connection isn't loading content fast enough to play. So, the switch from playing to paused, can happen through either internal or external events. States can also apply to smaller parts of a program, like the cursor in a word processor. When I first load up a document in the word processor or my tablet, there's no cursor. I can scroll through and read the document, but not write anything, which is good, because I must use my tablet for reading. To write anything, I have to type somewhere in the text, and a cursor appears, so I can start typing. But if I hold down my finger on a word, something else happens, the word gets selected, and the small cursor turns into a bold blue selection bar. So, the cursor has three states, absent, typing, and selection. Each of these will change the behavior of the program. If I type an x in the absent state, nothing happens. In the typing state, then an x will appear at the position of the cursor. But if I've selected my whole document, it might all get instantly deleted and replaced by a single x. So, it's very important to know what state you're in. This is one of many different examples of states in a complex piece of software like a word processor. There are different states for when a document is loaded, or when there's no document. Covered formatting is a state, because the texts will look different if you are typing in the heading state, from the normal text state. A lot of understanding how software works, is understanding which states it has, and what effect they have. A common problem when working with software, is that it suddenly stops working as you expect it to, and it starts doing something strange, or not responding. That is often because the software is in some different state that you don't expect. It could be that you didn't realize your entire document was selected. So, you'd be pretty shocked when it all disappears when you type a letter. Another example is email software. We open up our email app on our phone, and expect it to be in a state where we can read and write new emails, but the new email isn't necessarily there yet. The software will probably be in an old state, where all it has available are the emails you downloaded last time. It will then enter a fetch new mail state, in which it tries to download any new emails. This might happen behind the scenes, and you won't notice it if you have a good internet connection, because your email will appear quickly. But if your internet connection is very slow, or fetching mail fails, something might go wrong. The email program might not display mails that you know should be there, or it might only show some of the new mail. It might be unresponsive, it might even freeze. One of the biggest problems with internet connected applications is that they can get stuck in states, if the internet connection fails. This kind of failure can be unpredictable. For example, if the download failed straight away, the software might be able to handle it okay, and just give you a message saying the download failed. But if it fails midway through downloading an email, it might just freeze, because it doesn't know what to do with half an email. Understanding what states your software can be in, and what state it's in at the moment, can really help you understand what it is doing, and to get over confusing problems.