So maybe you need to do some sort of calculation to get that value. You have to yield back some value because remember the whole purpose of the switch expressions is to assign some value to something. It needs to be statements, but your final line a lot of that has to be yield. Let's say that you wanted to execute multiple statements because this one little arrow is really cute but what if you had to do four or five statements? You could probably just add curly braces and then you can write whatever you want to write. Notice they even have a little squiggly line, like, what are you doing here? All right, so if I had, like if I wanted to say the same thing for two cases, I would simply do a comma delimited list, and for both of those cases, it would print that. You don't want to like ever just kind of be repeating stuff like for example something like that, that's not good. Now if you want it the effects of maybe follow through or you basically just had two cases with the same code that you wanted to execute. Great question, any other questions? Let's run it and with a good case. So, we did in fact get an error, but at runtime. The switch expression does not cover all possible values. It does not have a compile error meaning it didn't complain before we run it, but there's a runtime error that says. I'm gonna run this without the default case. You want to see what happens if we put z I do all right let's print this out. And it looks like you don't need the default and you don't need to do some special initialization of it. Much more streamlined, right? All right, so let's comment out default. All right, and then we just end this with a semicolon. All right, we're gonna write the default one and then I'll comment it out. You don't need break statements with switch expressions because there is no fall through, all right? So this is not even needed, all right? So let's go ahead and clean all the rest of these up. So I'm gonna erase message=, and we'll just put Excellent job. And then I put the mess, whatever I want the message to be. So it's a bit more streamlined, instead of this :, after the case, I use a- and an >, a caret, right caret, to make an arrow. The way that the cases are written changes a bit. So I'm gonna switch on grade, I still have my curly braces and all of that. So I can say I can just move this switch up to the right hand side. So instead I can say string message equals and then on the right hand side of this statement, I would use the switch. This is like a lot of bulk in order to just assign something. So right here where we have string message what we're doing in this which structure is where assigning a value to message well You can do all of this with an expression expressions are specifically switch expressions were specifically designed for this sort of switch statement, right. So we'll just do _switchexpressions All right. And if we go over to the project let's right-click on grade message and choose copy and then on decision structure paste and they'll ask you to rename this because you can't have two classes with the same name. And we're gonna use a switch expression to print the message versus a statement so you can see the difference, okay. All right, so we're going to copy that code we just wrote will copy it into a new class. So these are similar to the switch statements, but they allow you to directly assign a value to a variable.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |