Lesson 5.2: If-Statement cont'd.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[MUSIC] Okay, I promised that we were done with guess my number. But I didn't say we were done with if statements. I still need to show you an example with multiple else if branches. I've got one here. It's called day of the week. And I can pop it up in the editor just by double clicking it, and here it is. It's a little longer, so I'll need to have some more space, and I can get that by just stretching the editor window down like this. Woah, how long is this thing woah, not much command window left, but at least we can see all of our function. Our new function will the print the name of the day of the week when we call it and give it the number of the day of the week as an input argument. It's number n. And it will also identify it as either a weekday or a weekend day. We have a long if else if else statement here, because it has six else if branches. So, counting its if and its else branches, it has a total of eight branches. After this long if else if else statement comes a separate if else statement. As I've mentioned, the function takes n, the number identifying the day of the week, as an input. The long if else if else statement then checks n, comparing it one by one to the numbers one, two, three, four, five, six, and seven. When the first true condition is reached, let's say n is three, we would print out the day of the week. In that case, would be Tuesday. And then, we set day type, a variable called day type here, equal to one, if it's a weekday. Or two if it's a weekend day, here is a two, here is a two. The else branch here is just there to handle errors. If the input is not one of the integers one to seven, then none of these first seven conditions can be true. So we take that else branch. And in that branch we print out an appropriate error message. Right here. And then we return from the function. We'll say more about this return keyword a little bit later. This second if statement, this if else statement down here is here to tell the user whether the day is a weekday or not. It checks the day type variable, and if it's equal to one, it prints, which is a weekday, otherwise it prints, which is a weekend day. In either case after the y here of the word day, we included a backslash n. This is the escape sequential, remember, that puts us to a new line. We wanted to get ready for the MATLAB prompt. Notice, though, that we omitted the new line in all the fprintf statements above. And we did that so when the name of the day and the comma are printed out, this text following it, which is a weekday or which is a weekend day, will be on the same line. Notice one last detail about the printing, we included a space here before the switches so that there will be a space between this comma after the weekday or weekend day name is printed, and this phrase, which is a. Well it's time to try this function out. Well, we'll note that this is an American function. The week starts on Sunday. And, that looks good. Wednesday is what we would expect. We would put in a 4. Let's see, Sunday, Monday, Tuesday, Wednesday. Yep, that's right. And we can see that Sunday is identified properly as a weekend day. And Wednesday is a weekday. So far, so good. So, we've tried a weekend day. We've tried a weekday. Now, we need to make a mistake and see if we handle errors properly. [SOUND] Well, it handled the error just right. It's passed all the tests. I'd say this is a working function. Well, it handled the error just right, and it did that because of the return statement inside this else branch. Return is not only a keyword, it's an entire statement. And it does just what it's name suggests. When MATLAB executes the return statement, it immediately quits the function and disregards all the other statements following it. Most typical use of the return is exactly what we're doing here. We encounter an error. And it's time to quit the function. Functions normally quit only when they reach the end of their statements. But you may want to have a function return earlier, either because of an error, just like this case, that was detected before the end of the function is reached, or simply because the function is done with its work before then end is reached. In either case, the return is what you need. Let's just remove the return statement for a second and see what happens with this bad input. I'm going to hit delete here. So, let's just run it again. Since that's the last thing we did, all I have to do is hit up arrow and return. And what did we get here? Let me stretch this up a little bit so we can see better. We got the error message, Number must be from one to seven. But then this is an ugly result, here. This ugliness happens because day_type is not assigned a value when the input is out of range. And, none of these else ifs or this if happens, and so, none of these assignment statements take place. day_Type equals. Since it doesn't have a value, MATLAB has forced to step in and take charge. This sort of behavior is termed an ungraceful exit. And it's an embarrassment for any self-respecting programmer. But with the return statement in there, and let's put it back. There. The function ends gracefully, and that's how all nice functions behave. So let's do it one more time. There. Return statement is a convenient way to handle this situation. And indeed, it's a convenient way to handle any situation for which there's nothing more that a function can do or needs to do. We've seen quite a few variants of the if statement. Let's summarize what we've learned. The simple if statement has a condition and a block of statements that's executed if the condition is true. If the condition is false, then the statements in the block will not be executed. The if else statement lets you specify a block of statements for the case when the condition is false, also. So, there's a separate block of statements for each of two cases. That is true or false. Because of that else branch, exactly one block of statements will always be executed. The if else if statement lets you specify multiple conditions that are checked one by one until the first true condition is found. Then, the corresponding block of statements is executed, and MATLAB skips to the end. If none of the conditions is true, then none of the statements in the blocks will be executed. The if else if else-statement is very similar to the if else if statement. The only difference is that we add an else branch after the last else if branch. The else block at the end will be executed only if all the previous conditions are false. But because of that else branch, exactly one block of statements will always be executed. [MUSIC] [APPLAUSE]
Info
Channel: Fitzle LLC
Views: 33,093
Rating: 5 out of 5
Keywords: MATLAB (Programming Language)
Id: rEarTHsM5dY
Channel Id: undefined
Length: 8min 33sec (513 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.