Lesson 5.1: Selection (Branching) in MATLAB

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[MUSIC] >> Hi and welcome to lesson five, Selection. [SOUND] This week we'll be talking about how to make your program run differently based on different conditions. So far MATLAB has executed the statements that you gave it in the order that you gave them. In other words, it used sequential control that's its default behavior. Sequential control is the most natural and the most common sequence in any program written in any programming language. And it's the primary example of a control construct. A control construct is just a method used by MATLAB to select the next statement to be executed after its done with the current one. In this lesson, we'll introduce some constructs that tell MATLAB to select its next statement based on some condition. A construct that does this is called selection or some people call it branching. The most common selection construct is the if statement. Let's introduce it with an example. The guess_my_number function is a single if statement. It starts with the if keyword, followed by the condition, x equals equals 2. This pair of equal signs is a new operator it means equal to or is equal to. The statement that follows the line with the if keyword will be executed if and only if this condition is true. In this case, if and only if x equals two. The if statement is terminated by the end keyword. Let's see this function in action. And here it is in the editor. Let's try it with a few bad guesses. How about running it with 3? [SOUND] And how about 0, nothing visible happens. And that's how it's suppose to be since our function doesn't do anything with a bad guess. Evaluates the x double equals 2 condition here and if it's false, as it would be, if x was 3 or 0, it skips this fprintf here inside the if statement and then function returns. Now let's try it with a correct number, we should see something different. [SOUND] Indeed we do, we see the congratulatory print out. The condition was true this time, x is 2, x double equals 2 evaluates to true, so we executed the fprintf statement inside the if statement and the function ended again. The control flow of the if statement is best illustrated by a figure. The blue dot represents the control statement, in this case the if statement. The blue lines with the arrows show the possible paths that MATLAB can follow. The branch that goes off to the left represents the case when the condition is true that is x equals two. The box labeled block represents the set of statements inside the if statement that will be executed if this branch is taken. The right branch represents the case when the condition's false. In that case, we go straight to the next statement after the if statement that is after the end keyword that terminates the if statement. This figure highlights in red the flow of control when the condition is true and as you can see in this case MATLAB executes the block of statements inside the if statement. This figure shows the case from the condition is false. This time the red lines shows that we skipped the block statements inside the if statement and jumped past it to the statement after the if statement. What if we want to do something when the condition is false as well? In other words, what if we wanted to execute one set of statements when the condition is true and a different set when it's false? Let's improve our guess my number function just a tiny bit by trying to cheer up the poor user who picks wrong number. Both the function and the if statement start out the same way as before. But after the first fprintf statement we see a new keyword, else, this else has 2 roles. First is signifies the end of the block of code that needs to be executed when the condition is true. And second is signifies the beginning of the block of code that needs to be executed when the condition is false. This new statement is called an if else statement. As you can see, our new and improved gues_my_number function prints out an encouraging message if the user's guess is wrong. The control flow of the if else statement is illustrated by this figure. Here again, the blue dot represents the control statement in this case the if statement, the if else statement. And again the blue lines with the arrows show the possible paths that MATLAB can follow through the statement. The branch that goes off to the left represents the case when the condition is true, that is x equals 2. The box labeled block1 represents the set of statements that will be executed when this branch is taken. These statements are the ones that come before the else keyword. The branch that goes straight down represents the case when the condition is false, that is x is not equal to 2. The box labeled block2 represents the set of statements that would be executed when this branch is taken. And these statements are the ones that come after the else keyword. This figure highlights in red the actual flow of control when the condition is true. And as you can see in this case MATLAB executes only the block of statements that comes before the else keyword. And this figure shows the case from the conditions files, this time the red line shows that we skipped the statements inside block1. And instead execute only block2, which is the set of statements after the else keyword. Okay, let's try this function in MATLAB. Here's our new and improved version in the editor. Let's take a wild guess and guess 3, like we did before [SOUND] and well this time we get a little bit more information. Not right but a good guess, MATLAB of course evaluated this exact same condition. 3 is equal to 2 and again came up with false but instead of just quitting this time it went to the statement or the block of statements in this case just one of them immediately after the else keyword. And that brought us to the fprintf which printed out this little bit more encouraging message. Now what happens if we guess right? Well let's do that just go right straight to that and we get the congratulatory message. The opposite happened this time the condition evaluated to true. So the statement immediately after the condition executed. To recap, when an if else statement is in our program we're guaranteed that it will execute exactly one set of statements either the statements between the condition here. And the else keyword here and that's carried out when the condition is true or the statements between the else keyword and the end which closes the if else statement when the condition is false. Okay, that's it for the if else statement and guess what? There's one more version of the if statement and here it is in the third version of the guess_my_number function it's the last one and that's a promise. One change it has nothing to do with the new version, just the number 2 here seem boring so we changed it to 42 which I'm sure you'll agree is far more interesting. More importantly we see a new key word here, else if. This is an if, else if, else statement. There has to be a condition after the else if in this case we have x less than 42. Just as there had to be a condition after the if key word, x double equals 42 in this case. You can probably guess how this thing works. Let's suppose x is equal to 42 then we carry out this statement here and we're done. Let's suppose it's false then we skip that and we come to the elseif and we check if it's less than 42 we carry this out. Let's suppose that's false then we come down and we do the else part. Let's call the function with 41. [SOUND] The first condition, when x equals 42 is false so MATLAB checked the second condition, the one after the elseif keyword here. That turned out to be true so we printed the too small message. If we run it with 43, [SOUND] you can see that we get the too big message that's because x was neither equal to 42 or less than 42 so both of these failed and we went to the else condition. A few remarks about this version of the if statement. First, the else branch is optional just as it was before. You can simply omit the else and close the if elseif statement with the end key word and if we leave that branch off like this. I'm just going to hit delete now, we're left with an if elseif statement. Second, whether you have the else branch or not you can have as many elseif branches as you want. They come one after another, right in here. We'll see an example of this a little later. Okay, counting the if elseif statement that you're looking at right now, we've seen four versions of the if statement. An if, an if else, an if elseif else, and an if elseif. To understand how these various versions of the if statement are related you can think of each of them as a fork in the road when you're driving. The most complicated one is the last one we saw before this one, I can get back to that by coming up here and hitting this undo button and there we are. This is a fork in the road with three branches one, two, three. You're going to drive down one of these forks to get to the next statement in the program. If you drop the elseif, Again i'm going to hit the delete then there's just two branches. If you add more elseif's there will be more branches and regardless of how may elseif's there are even if they're aren't any at all. If you get rid of the else then when all the conditions are faults you'll just detour around the fork and drive directly to the statement that comes after the if statement. [MUSIC] Okay end of remarks and end of all these versions of guess my number. [MUSIC] [SOUND] [APPLAUSE]
Info
Channel: Fitzle LLC
Views: 45,146
Rating: 4.9714284 out of 5
Keywords: MATLAB (Programming Language)
Id: rzm_FyZ9SmA
Channel Id: undefined
Length: 11min 53sec (713 seconds)
Published: Sat Jun 13 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.