12:33:18 In this module, we will be learning. A new concept, the idea of branching. What if? 12:33:25 Now decision structures allow us to leave that linear path and create a branch. But we will still return to one ending stop. 12:33:36 So this is important to note. Programming rule number one, the process linear still follows. But based on a decision, we may take one path or the other, which will eventually reconnect to stop. 12:33:53 So let's get started, shall we? 12:34:04 So the first thing that I would like to note. Is some of the operators that we are going to use. 12:34:10 I, arithmetic and logic operators. But more specifically, the logic operators, okay? 12:34:17 So our logic expressions evaluate to true or false true being one, false being 0. And these are the decisions, the values that we're going to use to make our decisions. 12:34:31 So where do we see those logic operators? We have our logical not with just the explanation point. You will see our online for our less than our greater than our less than or equal to our greater than or equal to. 12:34:51 Online 5. Our question are they equal to notice the double equals sign that is asking the question is the left hand side equal to the right hand side it is not an assignment. Is the left hand side equal to the right hand side? 12:35:06 It is not an assignment. And are not equal to on line 5. 2 other operators, compound operators are the and with the double ampersand. 12:35:18 Okay. And that means both conditions must evaluate to true for the statement to be true and our logical or online 7, which means. 12:35:28 It least one of the conditions must be true for the entire statement to evaluate to true. Now at this point I would like you to pause the show for a moment and look at the example column step by step for those logic operators that you're seeing online. 12:35:47 One. And mine 4 line 5 line 6 and line 7 look at those examples please read them very carefully. 12:36:41 Help you to understand how those logic operators work. Now I'd like to add just one more logic operator to the screen. 12:36:50 This is exclusive or or XOR. The rule for exclusive or is exactly one statement must be true, but not both. 12:37:00 In the place of or on line is 7. At least one of the statements must be true, but both may be true. 12:37:10 Exclusive war says only exactly one must be true. Now some of this logic is also being covered in your discrete structures course and it should have been covered in your algebra course to get to this class. 12:37:23 And that was one of the prerequisites. So let's look a little bit more about that Boolean data type because it relates to these logic operators and it is the data type that will be using as we enter this concept of branching. 12:37:42 So things to know. You can use logic expressions in the assignment of a bullian variable. So for example, let's say we have an integer number and it's equal to negative 5. 12:37:54 Now I'm gonna have a Boolean variable called is positive. And I'm gonna set it's assignment to the answer is number greater than 0. 12:38:04 Well, negative 5 is not greater than 0. So is positive would be set to false. Do you see how that works? 12:38:14 Now, you can use Boolean variables alone or as a comparison to true or false. So let's say we have Boolean acts and we set it to true, we assign it to true and then we can ask and here's that new word we're going to learn on a branch. 12:38:31 It's called If. And we can say if x, so in other words, if true. We'll do some sort of task and we'll see a little bit more of the code in a moment. 12:38:41 Now that statement can be also written as if x equal equal true. The latter statement is probably a little bit more easy to understand as your first programming, but both of those statements will perform the same thing, whatever the value of X is true, it executes that if branch statement. 12:39:02 Good access false, it does not. Now we'll see a little bit more of that in a moment. 12:39:08 Right now we're just looking at some things to know about the Boolean data type. 12:39:14 Little bit more. If we're going to take user entry, so in your hello world to program you got an integer value from the user for their age. 12:39:25 There and you use next. Well, there is a method called next Boolean that will take a Boolean value from the keyboard. 12:39:35 Now, the keyboard is being used by a user who is not necessarily a programmer. So in other words, next Boolean will take the words true or false in any format of combination of uppercase and lowercase letters. 12:39:53 And then it is case and desensitized all to lower case. So as you can see in the example, any combination of those letters as long as they spell the words true and false will work from the keyboard. 12:40:09 Now you as a programmer, if you are hard coding the literals, in other words you create a Boolean variable and you want to set its initial value to true or false. 12:40:20 You must use all lowercase. So user entry is a little bit more forgiving. As a programmer you must code them in in all lower case. 12:40:35 Now, when we look at evaluations of compound statements, the computer does something that's called short circuit or lazy evaluation. 12:40:46 So for example, If I have 3 Boolean variables, I have X, it's set to true, Y is set to false and Z is that to true. 12:40:57 If I have a statement like this, if X or Y or Z, that means it's at least one of them is true, the whole statement is true. 12:41:08 Lazy circuit. Lazy evaluation. What happens is it looks at that first value acts. 12:41:15 X is true. Now the OR condition only requires one statement to be true. So it never bothers to evaluate why and see. 12:41:25 In other words, it says, yep, this is true and it begins to execute the if branch. 12:41:29 Please note that there will be questions on the final exam as well as in an upcoming, activity that I will have later on in the course to test that you remembered this. 12:41:42 So Y and Z in this case are never checked. It doesn't matter what their values are because X was true. 12:41:49 It stops evaluating. Now let's just assume for fun that X was false. Well, then it would jump to why it would see why is false as well. 12:42:00 And then it would finally check Z, which is true. But if anywhere along that chain. From left to right, the value is true in this war situation. 12:42:09 It stops evaluating. 12:42:12 Now, for the and remember the ant requires both statements to be true. So if we have it's Y and Z, well, we look at Y, Y is false. 12:42:24 So already it would be impossible for both statements to be true. And so Z is never evaluated. It is skipped and that statement is false and the if condition statement is never executed. 12:42:37 Okay, so that's short circuit or lazy evaluation. Okay, now let's say for some reason in your code you don't want that short circuit or lazy evaluation to happen. 12:42:51 Well, there you can use instead the single bar or the single and this does complete evaluation. It's not necessarily needed for all applications. 12:43:03 But I just want you to be aware most cases we use the double bar in the double and for sand, but there is complete evaluation using the single bar or the single ampersand. 12:43:15 And so for these examples, the results end up being the same, but all of the variables are checked. 12:43:23 I will have a fun activity later on in the course to test that you remember this information. So what does that mean? 12:43:30 Probably circle it highlighted in your concept guide. Put a little posted note on that page in your notebook so that you can come back and refer to it. 12:43:42 So now let's start the concept of branching. Because we're going to be using this Boolean data type heavily in branching. 12:43:51 So the code structure that we've learned so far in the course is programming rule number one, the process is linear. 12:43:59 It's a sequence structure. One linear path from the start of the program to the end. And our flow charts preserve that and we can see it very clearly. 12:44:08 But now we're gonna discover that sometimes we wanna do one thing if some condition is true and we wanna do something completely different if it is false or maybe we don't want to do anything at all. 12:44:22 This is when a decision structure comes into being. So it's at a point when the outcome determines which segment of code should be executed next. 12:44:32 Now remember, those 2 paths, eventually if you look in the picture, they reunite and continue as a singular path. 12:44:39 So a branch allows us to go in 2 different directions, but in the end we will still come back to one stock. 12:44:47 So please make a note in your flow charts. There should always be only one start and one stop. 12:44:53 Because programming rule number one, the process is linear still exists. This just allows us to branch to do different things based on a decision, but we still come back in the end to one unified. 12:45:11 So for example, Let's create, kids math quizzer day. And so our specifications are we're going to ask the user to solve the simple math problem. 12:45:22 And we are going to reward correct answer. So if the answer is correct, we're gonna say, yay, good job. 12:45:29 Okay, so that's our rules. Now we're gonna see that this might not be the best program ever, but let's start with this. 12:45:39 Okay. So this is the if statement flow structure. So now you're going to be introduced to a new shape. 12:45:45 Now I'm only showing you a flowchart segment. So in other words, there's more at the beginning and there's more at the end. 12:45:54 I'm just showing you this segment utilizing the if statement. Okay, so there's our problem. 12:46:01 We have a kids math quizzer. We wanna ask the user a simple math problem and we want to reward the correct answer. 12:46:08 So that's our specifications. So here, for example. We output, we might ask, hey, what's 4 plus 5? 12:46:15 What does that equal? And then as always, that's that 2 step we prompt for some information and we store that information. 12:46:24 So the we're going to input and here's our variable identifier answer, right? A and S. 12:46:31 And so, from the keyboard, the answer will be put into AMS. Now, here is our new shape. 12:46:38 If A and S is equal to 9, right, 4 plus 5 equals 9. So if that's true, we are gonna reward the student. 12:46:48 If it's false, we're not gonna do anything. So what does that look like in a flowchart? 12:46:52 It looks like this. So the if is where we branch. You're going to see 2 branches. 12:46:59 Those branches must be labeled. So see how we label them? We either have the yes true path or the no false path. 12:47:06 I prefer that you use the words true or false. So answer does equal 9. This is true. And so there was our output statement. 12:47:14 We're gonna say output correct answer. Yay! Now notice the path after correct answer. Where do we go back? 12:47:24 To after the if statement. So we're going back to our linear process. Now let's say we the next part is we're gonna ask another problem. 12:47:32 And then we could continue this cycle for as many problems that we wanted to ask. Now this is a simple if statement. 12:47:42 The false branch does nothing else. We only veer away from our linear path. If the if condition is true and we will execute one line of code. 12:47:54 So important to know if the if statement is true it will only execute one statement after. If we want to do multiple statements, we're going to use a curly brace, okay? 12:48:08 So let's see that. 12:48:14 Here is the syntax for what we just did. Okay, so there's one statement. Now if we want to have multiple statements, do you see how I added the curly braces? 12:48:25 This will allow that to be treated as a as a compound series of statements that will all be treated together. 12:48:34 So if a condition is true and we have multiple commands we want to execute, we will have to place them within the curly braces. 12:48:42 Again, I highly always recommend that that closing curly brace and I will pause the show for a minute here and get a pen out. 12:48:50 Okay, that you put a, there's my worst drawing with my mouse. End of it right And so I will write a note to myself here and it's worth it to go this slow. 12:49:06 So I emphasize and I apologize for the pen going scribbling like that. But end of if. 12:49:13 Okay, so what happens is we'll be adding more and more curling bases to our program. And if you enter the starting one and forget to put the closing one, you won't get past the compiler and mismatch curly braces are extremely difficult to find. 12:49:30 So in good programming form, I always encourage you when you open a currently, close it, then go back and put your content in in the middle. 12:49:40 Okay. This is a mistake. If you say if answer equal equal 9 semicolon, what you have just said is if this is true, do nothing. 12:49:52 So between their closing parens and the semicolon, you're basically putting no coming and you're saying, do nothing. 12:50:00 Now there are cases where we write the null statement for the if. But it's not necessary. 12:50:05 So do you notice here if answer equal equal 9? Here is our semicolon to say this is what I need you to do. 12:50:17 Now in the compound statements, there can be multiple statements in there. The curly braces keep them together as a unit. 12:50:25 Okay. So. Sometimes we're doing something very simple with a branch. 12:50:32 And so we are only doing one statement. But I have found that in good programming form, every time I have an if statement or an L statement or later when we get to looping. 12:50:43 I just put the pair of brackets in there because sometimes you'll add a second statement. And you want it to be handled as part of the if statement, but you forget the curly braces and next thing you know you've got a logic error and your program is not doing what you best it to do. 12:50:59 Now note these little ellipses just mean there's code above and below this code snippet that I'm prior to. 12:51:08 Now, this was a simple math quizzer. But, it has a quizzer, it's probably not very good, is it? 12:51:14 Because just rewarding the students that were successful does not necessarily help the students that needed the most help. So could we make this better? 12:51:24 Well, yes, we could. What if the student that has the answer incorrect? We tell them what the correct answer is to help guide them. 12:51:33 Well, if we were to do that, we would use what's called an if else statement flow structure. 12:51:40 So in other words, if the statement is true, we're going to do something else. If it is false, so the else handles the second path of the command, meaning if the if statement is false, here's what you should do instead. 12:51:55 Now it looks very similar to start. We asked the question. We gather the data. 12:52:04 We check the answer. Now we know on the true path what happens, right? Correct answer, yay, but on the false, you'll notice I put the word else. 12:52:13 So the false branch becomes the L statement. Now the old statement works just like the if that it'll execute only one line of code if the if statement is false. 12:52:25 What if we want more than one statement? Again, we're gonna use those curly braces. 12:52:30 So in this example. Here's our else. We're gonna output sorry 4 plus 5 is not is 9 not and whatever their answer was. 12:52:42 Now notice the panel. Both the if branch and the else branch reunite under the let's try another problem. 12:52:51 So we are back again to that linear process. This is important to know why we're gonna only reach one stop. 12:52:58 Now what does this look like, Well, here is our syntax, right? So if answer equal 9, we're gonna system out front line correct answer. 12:53:10 Now notice the else. So the, And in this case, I had 2 statements. I had sorry and correct and here's what the answer should be because there are 2 statements I have to use those curly braces. 12:53:27 Otherwise, the else would only execute that first output statement and the second output statement would be at continue in that linear flow and we don't want that to happen. 12:53:38 Now notice on this slide I am demonstrating for you using those comments to mark your curly braces. 12:53:44 That is critical for your success. So here's our single statement. Here's what we call a compound statement. 12:53:53 The curly braces preserve those statements that they must be treated as a unit. Okay, so let's take it up a little. 12:54:04 Let's say we want to convert. The temperature from Celsius to Fahrenheit. 12:54:09 Or Fahrenheit to Celsius. So a simple flow structure and again I'm not giving you the entire flowchart but I'm giving you a proportion of it. 12:54:21 It would be something like this program, give a little description as to what the program is gonna do that is gonna convert temperatures. 12:54:29 At some point you're gonna have to collect information. So I've left that off. 12:54:35 So we have to say what the program does, your next task is going to be asked some sort of question. 12:54:42 Is it true you want to convert to and then grab a Boolean variable from the keyboard and then if it is true You will do what? 12:54:54 Get the Celsius temperature. Converted, there's the formula. Be careful with parentheses order does matter order of operations for this in order to get the correct answer. 12:55:07 But if it is false, we'll assume that it's Fahrenheit. 12:55:11 And we will get the fair night, answer. Now notice that both of them reconnect to display answer. 12:55:21 So in other words, I'm using one variable called answer. And if I take the true path, I calculate answer one way. 12:55:28 And if I take the false path, I calculate answer another way. And then after that, I come back to the display answer and I continue the flow. 12:55:39 So you are to use Lab Doc One as a starter. You're going to need a full flowchart. 12:55:45 My expectations are that you show the user, an output statement that says what the process is going to do. 12:55:55 That you've done asked them which direction they want to convert to either from Celsius to Fahrenheit or Fairy and I to Celsius. 12:56:02 I would suggest grabbing a Boolean variable for that question, but you may use an integer or however you want to figure that part out for yourself. 12:56:11 Then you're going to base the decision though on some sort of Boolean variable. I've given you the path for the branching structure. 12:56:22 And you'll need to complete the full flowchart, complete the full code and turn in the lab. 12:56:29 You will note that in the website, this is one of your 2 point labs for this week. I suggest at this point that you pause the show. 12:56:39 Work on this lab. And then continue the rest of the show. 12:56:50 Welcome back. So these decision structures can actually be nested inside of one another. Or the nested if. 12:56:59 So perchance, what if we had a problem and in order to qualify for a loan, you must earn at least $30,000 annually. 12:57:08 And be employed for at least 2 years. So in order to have some nice output statements as to where someone may not qualify for a loan, let's look at this nested if structure. 12:57:23 So the first thing is let's assume that we've gathered a variable and an identifier called salary. 12:57:30 And we have that piece of information. And we also have gears employed. Okay, and so one of the first decisions we have to make is is the salary greater than or equal to 30,000 that means yes you could qualify for the loan but you also have to pay one other test. 12:57:46 Okay, so yes, that's true. But no, at this point, we can give a nice error message that said, I'm sorry, you do not qualify for this loan because your income is too low. 12:58:02 Let's go to the true path. They do have a salary greater than 30,000. Okay, so then our next question is years on the job. 12:58:11 They have to be employed for at least 2 years. So if that's true. Great, you qualify for the loan. 12:58:18 But what if that is false? Well, that means their employment history is not long enough. So we can display your work record is too short to qualify. 12:58:27 Now notice all of these rejoin. And we continue the linear process. So in other words, the if years on job is greater than or equal to 2 is nested inside the if condition of salary greater than 30,000. 12:58:47 So we write that if statement and then we have a curly brackets that say if yours on the job is greater than or equal to 2. 12:58:55 So let's look at that code. Okay, so do you see the brackets? This is very critical that Now you're starting to see lots of curly braces and it's very easy to get lost and mismatch them. 12:59:09 That's why labeling the closing one is critical. So you see the if salary is greater than equal to 30,000 I use the color coding here of gold Here is our nested if with the if years on job is greater than 2. 12:59:24 There's the true statement. Yep, you qualify. Else, you haven't worked long enough. 12:59:30 But notice the matching else for the if salary greater than 30,000. It is outside of the series of curly braces for the if condition. 12:59:43 Do you see that that final else is at the bottom? This is important to note. So we don't put that else immediately. 12:59:52 It follows the closing. Of the nested if. Please pause that for a moment. Take a real close look at that slide. 13:00:01 In case you have some difficulty. Now, we have We have if else we have if with if else conditions nested inside of them. 13:00:12 We also have what's called the series of if else if else. Yes. This can go on and on. 13:00:19 So for example, We have a problem. We have a user that needs to enter. We have a test score and we want to display what their letter grade is. 13:00:31 Okay, and so 90 or better is an A, 80 to 89 is a B, 70 to 79 is a C. 13:00:40 60 to 69 is a D. And less than 60 is a. Now, a score greater than 100 is in Valley. 13:00:49 Okay. And so the program will display an error message if there's an in So it looks something like this. 13:01:00 So scores less than 60, if that's true, you have an F. Else if so at that point you see how I'm using the words here so the first was an if scores less than 60. 13:01:13 Then, else. Yeah, score is less than 70. We have a D. Els? 13:01:25 Yeah. Score is less than 80. Well, you can figure this out now. It's going to be a C. 13:01:32 That's true. But if it's false, else if Score is less than 900, that's our B. 13:01:43 Okay. Now what happens? 13:01:50 At this point, if that is false. If score is less than or equal to a hundred. Because scores over a hundred are gonna be invalid. 13:02:00 Here's our score of A. Else Our last else is We have an invalid entering. Now notice all of these will then reconnect and go back to our linear process. 13:02:14 Does that make sense? So these, this is a decision structure. The dilemma with it is We should probably get validate data. 13:02:27 And so this is your first, introduction to the idea that anytime we get data from the user. 13:02:34 We should first validate it that it's data that we want to work with. So in other words, valid data for a test score is in the range of 0 through 100 inclusive. 13:02:46 That means it's 0, including 0, including 100. So one of the first things we probably should have done before we went through this chain was to ask if score is less than 0. 13:03:00 Or score is greater than 100, we should display invalid entering. So that is something we would have to do to fix this if else branch. 13:03:11 Okay. And let me make sure I think I'm gonna show you that. Yes. So you can see what I did is instead. 13:03:22 My very first question is to validate the data. And so if the score is not a valid score, I display invalid. 13:03:33 And then I start my, yeah, you know, else if sixtys and then down the chain. Now look what happened to the end. 13:03:41 Let me backtrack for a second here and show you the slide. Do you notice we had to check if score is greater than that? 13:03:47 Is less than or equal to a hundred and we had a true and false branch on that last one. 13:03:52 Well, because we restructured this, look what happens after 90. So score is less than 90, it's a B. 13:04:00 But the only way we could get into this chain is the score had to be in the range of 0 to 100. 13:04:06 So if you notice that last statement after score less than 90, the else it must be an A the score must be a 90 through a hundred. 13:04:18 Do you see the difference? This is important to note, so being very careful of thinking about the data that is falling through the chain. 13:04:29 And when we get to that last We don't have to ask if the score is in the range of 90 through a hundred. 13:04:35 It must be in order for it to gotten to this point in the chain. I hope that is helpful to you. 13:04:42 Again, Well, we're gonna see here. Yes, some cold. This is again a lab doc. 13:04:52 All of this code you're staring at on the screen is also available as lab. 2, I believe, on the course homepage. 13:04:59 But if you notice my logic here is the first group of students I remove are students that with an app. 13:05:06 But when I teach a class, most of my students are so excited to learn and I have very few at students. 13:05:15 I have more A students. So in other words, I want you to flip the logic of this. To take care of the A students first rather than last. 13:05:25 So first find the A students, but before you find the A students also. Remove invalid data. So we're going to remove in valid state data and then we're gonna start the chain from A through app. 13:05:40 So in other words, in this lab assignment, what I need you to do is I want you to check for the letter A grade first and even before you do that validate the data to make sure it's in the range of 0 through 100 inclusive. 13:05:56 Again, for this one, I need you to flowchart it first, then code. If you notice, I'm introducing you to the J option pane. 13:06:07 So you're going to see pop up boxes for this one. So when you turn in your work to show output, you're gonna have to use, print screen or the snipping tool to screen capture those output boxes as they appear on the screen to turn in the output for this lab. 13:06:25 Questions, please email me. Otherwise, have fun with this one. 13:06:33 So. One other interesting thing I want to introduce you to today. String objects. Now remember we've been working with primitive data types up to this point. 13:06:47 And we saw the string, which, that's our flag that this is an object. Strings are nice because they allow us to store character strings. 13:06:59 Names and things like that. Data that we often work with in the real world. So this is important stuff to remember in your notes again, tab it off somewhere. 13:07:09 So the string method has things called equals compared to equals ignored case and compared to your case. Now the ones that say ignore case mean that the method itself D-sensitizes the text so that it's all lowercase. 13:07:27 Think about that example. A few slides back when I talked about the Boolean data type and the fact that the user can enter. 13:07:36 Uppercase, lowercase letters as long as they spell the words true and false, it will work. 13:07:41 That's what ignore case does. It desensitizes the words so that they will match up. 13:07:47 Now, these are. Methods and they return a value. Do you remember main is void? 13:07:56 The return type is void, so it does not return a value. Well, these methods will bring back some answers. 13:08:00 So you can see in the syntax, they'll one and veil 2 are actually string objects. 13:08:06 So in other words, I say. String val one. In other words, I've declared And. 13:08:14 Storage space of string object type and I've given it the identifier name, V one. 13:08:20 Same thing for Val, 2. So the syntax there shows you the call. Now let's look at those return valves. 13:08:28 So these methods have a return type. Remember main, it has the return type of void or no return value. 13:08:37 The methods of the string object, each return either Boolean or integer values depending. Okay, so equals In either case, returns to or false compared to returns a new miracle value, an integer value. 13:08:58 Okay, so let's take a look. 13:09:02 So. As you can see, equals and equals ignored case. If the values are equal, they will. 13:09:11 So the first one, if I had the word cat, lower case CAT and the word cat, lowercase CAT and the word cat, lowercase CAT. 13:09:18 Equals would return true. Now, if I had the word cat, lower case CAT. And the word cat uppercase C 18 equals would return false. 13:09:32 Equals ignore case would return true for both of those examples. Because it desensitizes that second cat that had the upper case letter C. 13:09:46 Now, compared to what it actually does is math. Okay. So if the values are exactly equal all the way through lowercase CIT, lowercase CAT, it's going to return 0. 13:10:01 If not, you're gonna see you're either gonna get a negative number or a positive number. 13:10:05 So what happens is in the first of letters that it doesn't match. So let's say we have the word. 13:10:14 Let's say we have the CBT and C A T. One happens is The 2 letters C's are compared, they match. 13:10:24 Moves onto the next letters B and A. So what happens is B. Minus A. Will B is a larger number, okay? 13:10:34 And minus A. And if you look at the ASCII table, you see those numerical values. 13:10:42 And what ends up happening if we have a positive number, then valet is greater than Velbe and think about it. 13:10:48 CBT versus C A T. B is a greater value. So you're gonna end up with a positive number. 13:10:57 If they are in alphabetical order, you will end up with a negative number. So go ahead and take a moment and look at the ASCII table and you can see when the words are the strings, they will match characters and the first time that the characters do not match or not equal. 13:11:13 The math will occur and it'll return the math value either negative or positive for that first match where they do not match. 13:11:23 Now ignore case, desensitizes it. So in other words, uppercase and lower KC get desensitized to lower case. 13:11:32 So the notice that the strings are normalized, but they will still work in the same way if it's in alphabetical order, even if it's a combination of uppercase and lowercase, it'll return a negative value. 13:11:46 And if they are out of alphabetical order, it'll return a positive value. So this is your first introduction to the string object. 13:11:54 Just because sometimes we are going to make decisions with strengths. Okay. There's one more operator I want to introduce you to that has to do with logic and, this uses 3 operands. 13:12:11 Okay, it's a ternary operator so use 3 operands and basically what this one does is we have some sort of expression that evaluates to true or falls. 13:12:22 We actually use the question mark. And if that expression is true. We executed expression too. So in other words, that's our if. 13:12:31 And this is our true statement. Then we have a colon. And if the statement is false, we execute expression 3. 13:12:40 So it's basically the same as if expression one is equal to true, execute expression to whatever it may be. 13:12:48 Else execute expression 3. Now I have some nice examples. Out there on the web for you already. 13:12:56 So please take a look at them. It was the conditional operator in action. You'll see I use it in output statements, that's usually the best way. 13:13:07 So like let's say we're sorting if you passed or failed a test and so on test score if test score is greater than 60 output pass otherwise output fail. 13:13:16 You'll see a very nice use for this conditional operator that's a ternary operator using 3 operands. 13:13:23 Okay, so the example is out there for you. So where does this operator fall in our order operations? 13:13:32 Down here, probably 7 and a half. So right after 7, but before 8. And so there's my little example there. 13:13:42 If X is less than 5. I'll put true otherwise output false. You'll see code examples provided in this week's module. 13:13:53 Okay, so. As we are wrapping up this section. Your assignment number 4 requires that you create a fictitious company. 13:14:07 Welcome the company and pretend you're creating the phone menu props. You need to create 4 to 6 departments. 13:14:13 Each department based on the user input. Now remember, user input can be in the range of 0 through 9. 13:14:20 They should reach a particular department. Now remember, if you only code, say 4 departments. Now remember, if you only code say 4 departments, then the other numbers in the range of 0 through 9 you need to handle as invalid entries. 13:14:33 So when you produce output for this, notice the submit your output. You need to run it for all input values. 13:14:41 So you run it for 0, then you rerun it again for one all the way up to 9. 13:14:45 So you're gonna have 10 runs of this program when you submit your output. Please make sure that you do that. 13:14:54 The this assign in specifications is also posted to the course homepage. You don't have to screenshot this. 13:15:01 You will find it on the course home page. Again, when is it due at the end of the module? 13:15:11 At this point, again, I would caught suggest if you pause the show, mark your time. Of the show and then continue the show. 13:15:19 After you do this lab. 13:15:31 Okay, welcome back. So what if you have more than 2 choices? There is another easy way. To do some branching and it's called the switch case. 13:15:40 So, our code structure. As a reminder, let's backtrack for one moment. 13:15:48 No matter what, programming rule number one still applies. There's one linear path from start to end of the program. 13:15:55 And our flowcharts help us to preserve that. We have now learned the tool of So we have those binary decision structures. 13:16:06 If something is true, we go one path, otherwise we take the L's path or we might just write an if that we do something if the is true. 13:16:14 Otherwise we continue in the flow. Okay. So. What if we have lots of decisions to make? 13:16:26 Well, what about those multiple options? What we would like to use is something called the switch case. So in other words, based on some particular choice, we will execute a specific section of code. 13:16:43 Now the switch case, flow charts pretty much the same way as the So for example here, I have a code snip, a flowchart snippet of an example for a phone menu prompt. 13:17:01 Okay, and so I have some output that is, output, and says press one for billing, 2 for technical support. 13:17:08 Free to change your password, for to speak to customer service. Representative 5 to exit. All other entries will display an error message. 13:17:18 So my case one means I'm matching billing. And I'll put my billing message, case 2 for tech, case 3 password, case 4. 13:17:29 Customer service and. Case default now this is something new as well here's another keyword The default usually handles the error messages. 13:17:42 So when we created the if else. Branches. We usually validated data first. In the case of the switch case, the default often handles the invalid data. 13:17:56 Okay. So. Let's, talk a little bit more about the details of using the switch statement. 13:18:06 Which is much better than a series of. So here's some of the hard fast rules for switch case. 13:18:16 The controlling expression, in other words, you know, that case one, that one. Must be either a character, a bite, a short, or in data type. 13:18:26 Okay, so because we're going to match that numerical value. Remember, why does character work? 13:18:33 Because characters are actually stored as into numbers. The case label must be a constant of the same type as a crowling controlling expression. 13:18:44 That's where I hard code in that one, right? So case one. If it matches that the variable matches that value. 13:18:54 Great. Then we're gonna execute that case. And we're gonna see the code in a moment. 13:19:00 The first I needed to give you the rules. Number 3, the dilemma with the switch case is once we match a case. 13:19:08 Togram in rule number one goes back into existence and soon as the case is matched code is processed in a linear fashion. 13:19:17 But we don't necessarily want to execute. Case number 3 or 4 once we've matched case 2. 13:19:24 So we must use the break statement. At the end of our statements for matching, for example, case 2. 13:19:32 We will then put a break statement which breaks us out of the switch case and allows us to continue the remainder of our code in a linear fashion. 13:19:42 Okay, so the break statement is required when using the switch case in order to break out of a case once it has been completed. 13:19:54 As a reminder, the default statement is utilized to catch invisible data input in good programming form. 13:20:03 So let's look at the syntax of this. So here we have a very, an integer variable input. 13:20:11 It's set to 3. Now remember. That can be our controlling, expression. Hey, he has to be an integer, a care character, a bite for short. 13:20:22 So in this case, it's in it. So here I switch on input. So whatever the value is an input. 13:20:29 When I match the case, I will execute that code. So notice the switch uses a series of curly braces, notice that nice comment at the end. 13:20:40 And here's what happens. If I match case one, I'd have whatever commands for billing and then I would break out, meaning I would leave. 13:20:48 Yeah, curly bracelet ends to switch. If I match case 2, I would have whatever commands for case 2. 13:20:56 Now notice case 2 in my example. I'm trying to look back at my example. Was, tech, one was tech support, one was changed password. 13:21:08 Well, both of them are gonna go to tech support. So do you notice case 2 has no commands, but at case 3, I list all the commands for tech support and change password because they're actually really going to go to the same department and then I break. 13:21:21 So in other words, when I match case 2. It's just gonna fall into case 3 in a linear fashion until that break statement is hit. 13:21:29 And then I fall out of the switch. 13:21:35 So there's my little note on that. Once the case is matched, we execute to the end unless we utilize their break. 13:21:41 So this is a nice way that we can combine those 2 departments with the same set of commands. 13:21:51 Now case 4. Again, I think and I believe in mine was to get the operator. So you've got some help. 13:21:59 And if you notice my case for also, falls into the default. In other words, they didn't enter one of my numbers, one through 4. 13:22:07 And so an incorrect selection. Or case 4 is still gonna take you to customer service and get you a real person to help you. 13:22:16 Okay, now notice for the case for and default, we are now at the end of the switch, so I don't necessarily need the break. 13:22:22 Because I am just going to continue the flow. There's no other commands and then I will be out of the switch. 13:22:30 Okay. So. No, this example is missing code commands to be executed once the case is matched. 13:22:41 The commands would be listed after the appropriate case statement. So in other words, I've just put comments in there as an example, you would be actually writing some output statements. 13:22:52 To display. So this is one of your remaining labs for this module. And what I'm asking you to do is to just revisit assignment number 4. 13:23:07 Now, the flowchart does not change too much. So you can really kind of look at your flowchart example. 13:23:16 For your original lab. Modify it slightly because the default is gonna hand the handle the Air messages. And I want you to code at this time using the switch case branching method. 13:23:30 Okay, again, you're gonna need output for 0 through 9. Again, when is this do at the end of the module? 13:23:39 I hope you've enjoyed this lecture on our. First new tool, branching. Please send me any of your questions. 13:23:49 Have a fantastic day.