So far, the programs we have been writing are great at performing calculations but they are limited and not like software that you are used to using. We are used to software making โdecisionsโ based on information. For example, when it is going to rain, Google Assistant (or Siri, or Alexfontconfiga, or whatever) can send you an alert reminding you to bring an umbrella. When it is going to be sunny, that same assistant might suggest wearing sunblock. The software underpinning those technologies has the capability to make decisions about what it tells you depending on the weather. I am certain that you can think of several other instances of computer programs making decisions.
Briefly, share with your classmates a unique way that a computer program makes a decision in some software that you regularly use. You can pick any computer program you like! For example, one of my favorite video games growing up was Mario Kart 64. My sister was WAY better than me at racing but the power-ups that you can collect change depending on what place you are in. Since I was usually in last place, and my sister was usually towards the front, the program made decisions to give me much more powerful items that allowed the race to be competitive! What is your story about computers making decisions?
The reason the scripts we have written so far seem so limited is that we havenโt learned a way to control the flow of programs or have them make any decisions. Controlling the flow and allowing our programs to make decisions is what we will learn in this chapter.
Our mantra and guiding process for writing complex programs will be: think, sketch, code, test, repeat. We will use what we learned about thinking algorithmically to make sure that we think before we do anything. We will sketch to ensure that we come up with a robust plan. After coming up with a sketch, we will write our code. Then we will test the code using as many absurd and wide-ranging possibilities as we can think of. Finally, we will repeat the entire (or part of) the process until our code works the way we want it to!
In this chapter, we will learn: how to combine what we learned about thinking algorithmically with MATLAB syntax to make complex computer programs. That means learning:
In my opinion, it is critical to follow along with this chapter, even more so than in previous chapters. Even if you have experience in other programming languages, the way that MATLAB handles loops and decisions is different. Make sure that you start MATLAB, create a new script, and follow along! It will help in two ways. First, you will get the experience from working on it today. Furthermore, by saving your script you can create good notes for future reference.