Also known as conditional statement, conditional statements, conditional constructs, if statement, if construct, if-then, if-then-else, conditional expression

Chapter 16: MATLAB Decisions - Engineering LibreTexts
eng.libretexts.org →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? 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. Now we are going to combine everything we have learned about MATLAB to allow our programs to make decisions. The trick is to combine what we learned about the relational and logical operators and thinking algorithmically to create what are called conditional statements. A conditional statement is an if-then statement (there are a few other flavors that we will learn about shortly, but this is the most simple one). We already saw examples of these types of statements in the thinking algorithmically chapter, but here are a few examples of conditional statements to jog your memory: The most basic form of a conditional statement is the If-End statement. Generically in MATLAB, an If-End statement looks like this: The generic flowchart for this can be found in figure 13.3 below: This is a perfect example of using our mantra: think, sketch, code, test, repeat ! Do not skip your brain workout for these sections. For each of the prompts below, I am expecting you to stop and actually work on this particular problem before moving on. Sketch - during the sketch phase, you need to draw a flowchart or write out a pseudocode for your algorithm that will solve this problem. The sketch phase will often require several repeats of the think stage. As you are sketching more questions may pop into your head about how to best solve this problem. Before scrolling down below, take a minute and try to sketch out a flowchart or write out pseudocode for this particular problem. Don’t skip out on giving it a try! Figure 13.4: Every time someone scrolls and looks at my flowchart before creating their own, this puppy doesn’t get a snuggle! Don’t be a monster! I was going to say the puppy gets hit but that is too dark. Look how cute he is! The flowchart that I created for this particular problem is located below in figure 13.5. Figure 13.5: My flowchart for the wage program. Note how there is both descriptive text and rough code. Take a look at the flowchart. Hopefully, it looks very similar to yours. Ask yourself the following questions before movi
~22 min read
If-then-else flow diagram A nested if–then–else flow diagram
In computer programming, a conditional statement directs program control flow based on the value of a condition; a Boolean expression. A conditional expression evaluates to a value without the side-effect of changing control flow.
programming language construct that performs actions according to Boolean conditions
Excerpt from a page describing this subject · 23,088 chars · not written by Vinony
via Wikidata · CC0
via Wikidata sitelinks · CC0
Discovered by embedding cosine similarity (sentence-transformers MiniLM, 384-dim).