Learn How To Use Decision Statements So Your Code Can Do Different Things And React To User Input
There are things computers are really good with and making decisions is not one of them. Are we any better at decision making? I like to think so. But does that make it true?
Ignore for a moment all the bad decisions we make each day because we only consider short term or immediate gain on impulse without really giving thought to the matter. I’m talking about the small decisions where we actually choose something.
Salespeople tell us that we buy something based on emotion and then justify the decision with logic later. While there’s probably a lot of truth to that, especially for big decisions, I don’t think that happens all the time. There’s something else going on. We’re complicated creatures, after all.
Many years ago I was grocery shopping with my wife not long after we had been married and we saw a display of watermelons. I picked one up and held it to my ear as I tapped it. That wasn’t the right one, so I tried another and did the same thing. Still wasn’t right so I tried another. I put that one in the cart when my wife asked me how I chose. My answer was simple. “It’s always the third one,” I said. She laughed.
You might think I was joking just like my wife did when I told her that. But there’s a truth to the process. In reality, all the watermelons for sale that day were probably good and there was very little to distinguish one from the other. If there was going to be a bad watermelon, then maybe one in a hundred or even less would be bad.
By picking up three watermelons, what I was actually doing was making sure they were all the same. And if they were the same, then why put down the third one just to pick up the first or the second one again? I was already holding the third watermelon so it went into the cart.
What would happen if I did find one that felt or sounded different than the other two that I tried? Then I’d know for certain which one to avoid. But as long as they’re all the same, then the choice is always going to be the third one.
Now, I ask you. Did I choose a watermelon? Sure, I made a choice to buy a watermelon. And maybe that desire was driven by emotion. What I’m asking is did I choose the third watermelon specifically? Or did I run a mental program? Not a computer program. But a human program.
I’ll go as far as to say that I have a well developed program that helps me to choose which watermelon to buy once I’ve decided to actually buy one. It uses the type of decisions that a computer can understand.
A computer doesn’t really make decisions. It just follows a program. That’s why I said that computers are not good at making decisions.
When we come across something unexpected, we can normally deal with it and decide what to do. When a computer comes across something unexpected, it crashes. That is, if you’re lucky. Sometimes, it might not crash but instead keep going with unpredictable results. Either way, it’s bad.
One of the most challenging aspects of programming is learning to anticipate decisions that the program will need to test for and handle. This class will show you how to write decisions into your code. This is sometimes called branching because depending on the result of the decision, your code can split just like a branch on a tree.
You’ll learn about assignment vs. equality. What does it mean to test for equality and other types of tests such as less than or greater than.
You’ll use the tests to write decision statements.
- Learn the purpose and structure of branching statements.
- else if
- When should you use braces?
- What is short circuit evaluation and how can you use it to your advantage?
- What effects does branching have on caching?
- When to use switch statement instead.
Switch statements are another form of branching and have their own purpose and way to write them.
And you’ll explore the return statement which also affects the flow of your code.