In this lecture, we'll learn how we can implement the selection control structure to make decisions in our program. We'll use the if statement to implement those decisions. So what kind of decisions might we want to make? We might want to know whether input provided by the user falls within a particular category. For example, did the user provide character input when we ask them to do so? Or did the user provide a positive input, a positive number when we asked for a positive number? Or even if we ask for any number and we just want to determine whether it's positive or not. We might want to print out a message based on a particular criterion. In an academic environment, GPA matters a lot to people particularly students. So we might print a message that says, if your GPA is less than 2.0, we print a message that says you're on academic probation depending on the school you're at. If your GPA is 3.5 or higher or 3.0 or higher, you might print a dean's list message. So you might want to print some message based on a criterion. You might also want to decide what menu to go to based on a user action if they're interacting with a graphical user interface and there's lots more. We have tons of decisions that we might make in our code and the if statement helps us implement the appropriate decisions about which path to take through our code. This is a picture of the most basic form of an if statement. So we will check some condition and I've expressed it as answer equal equal the character Y, which is a Boolean expression. We've discussed Booleans a little bit back when we talked about data types. A Boolean expression evaluates to true or false. This picture represents the fact that if that Boolean expression evaluates to true, then we're going to print some message, some yes message. If it evaluates to false, we're not going to do anything, we'll just continue through our program. Here's the code that I've already started writing. Here's my pound define for Visual Studio so I can use scanf. You don't need that pound define if you're using X code. I have my comment that says I'm demonstrating if statements and here's what I've written so far. I've declared a variable, that's a char variable for the user's answer to my question. I'm prompting for and getting the answer about whether or not the user likes pizza. Here, I've told them to answer Y or N so that they know what they're supposed to type as their input. For scanf, the format specifier is percent C and of course I always use address of and then the variable, I'm putting their response into this variable right here. So let's print an appropriate message. We'll start with an if statement like the picture I just showed you. The syntax for an if statement is if followed by an open and close parenthesis and within this parenthesis, I put my Boolean expression. Then I put an open curly brace and a closed curly brace. This will be the body of my if statement. So if this Boolean expression that I haven't typed in yet evaluates to true, then all of the code in the body of the if statement will execute. So my Boolean expression just like in the picture I showed is answer equal equal Y. So this equal equal is an equality comparison. So it's going to take whatever the user entered for answer and it's going to compare it to a lowercase Y and if this is equal to this, then the Boolean expression as a whole evaluates to true and we're going to execute this code. So let's say something like, "That's great. I like pizza too." So now if I run my code using Control F5, it asks if I want to build it so I will. It asks, "Do I like pizza? Yes or no?" I'll say, "Yes." As you can see, it says, "That's great, I like pizza too." I'm going to add some line breaks so this looks a little nicer. Let's try that again. "Yes, I do. That's great. I like pizza too." If I type anything else like no, then it doesn't print anything. So this sends us down the true branch. When this evaluates to true, we call those paths through our code branches. If this is false, I just skip past the body of the if statement and I get to here which is really just the end of my program. Now I will say that C allows you to do this. If you only have one line of code in the body of your if statement, then you do not have to put the curly braces. I strongly suggest that you always put the curly braces. Let me show you why. Let's say that later on you decide you want to add some additional information here. Even if you indented as though it's in the body of the if statement, If I Control F5, yes seems to work fine. Of course I need that new line again. But yes works fine. I'm going to ask this to no longer show that dialogue. But if I say no, it still says, "I like pepperoni." And you're sort of like, "I don't care. I don't even like pizza." The problem is that this is the entire body of the if statement, and this is just outside the if statement. That's why we use the curly braces for the bodies of our if statement. You will see lots of people not using the curly braces and that is acceptable C. The issue is that it only takes you a second or two to add the curly braces but you can spend hours trying to debug the kind of problem that we have without the curly braces. So just to take the time. My recommendation is that you always use the curly braces. At this point, we're only printing a message when the user answers yes. It certainly feels like we ought to be able to also print a message when the user enters no. Of course we can. So let's look at how we can do that next. Here's a picture of what we're going to do. We're going to have one path that we follow, the path we already follow if they say yes. We'll follow a different path if they actually say no. Okay. So the way we add that alternative is we type else. Now, we'll print the message for when they said no and I'll say, "That's okay. I'll eat enough for both of us." So now, we have the true branch, that executes if this Boolean expression evaluates to true, and we have the false branch that executes if this Boolean expression evaluates to false. I'll add my new line. So now, we'll still check to make sure it works correctly for yes. But if I enter no, then I get the no message, and that's great. Now, the issue is, if I enter q, it says that's okay, I'll eat enough for both of us. That's not really an appropriate response. We should tell them, that they entered an invalid input, rather than acting like they said no. The way we can do that, is we can take this else, and we can turn it into an else if. We put another Boolean expression here, like that. Now, the way this will work is, if this evaluates to true, I'll do this, if it evaluates to false, I'll come down here. If this evaluates to true, I'll do this. If it evaluates to false, I'll actually come down here. So I'll show you with yes, with no, and with q. So at this point, I still want to print that error message, so I can add an else at the very end. So we have one, and only one if, we have zero or as many as we want else ifs. That doesn't really make sense with a yes or no question. But if we had menu choices, and we were getting four different valid menu choices and then the rest are invalid, then in fact, what we would do, is we would have a bunch of else ifs, one for each menu choice. Then finally, we have zero or one elses. So here's where we say, if you gave us invalid input, like that. You'll notice our testing gets more and more complicated the more branches we add to our code. That's just a side effect of needing to test more thoroughly the more complex our program gets. So we'll check yes, we'll check no, and we'll check q. You might feel really good at this point because we've covered all the bases, we've covered yes, we've covered no, and we've covered q. We can make this a little nicer to the user though. Because, if we run the code and they say capital Yes, we're telling them I said yes or no. That's kind of being, right. We've told them yes or no, and so they should be able to enter both lowercase and uppercase y, to get the yes message. I'll show you two ways we can do this. First, I'll show you how we can make this Boolean expression a little more complicated to cover that possibility. So if answer is equal to yes, or this is the 'or' operator, remember we talked about this when we talked about Bool's. So if the answer is yes, or the answer is capital Yes, then we'll give them the yes message. So I'll control F5. We'll try a whole bunch. Lowercase y works great. Uppercase Y also works. I'll just show you I didn't break anything. So the rest works as expected. So that's one way that we can make our code a little nicer to the user, is by building a slightly more complicated Boolean expression. The downside of this is, if we have a whole bunch of else ifs and we're dealing with character input, then this means that we have to write a lot of extra code covering both lowercase and uppercase input. I'll show you a different way to do that. So I'll get rid of this, and let's go take a look at the C documentation. It turns out that the C standard library documentation is our friend yet again. Although this time, math.h isn't the one we want. So if we look at the descriptions for some of these, we actually see that the third one down here ctype.h, is a set of functions that we can use to convert between upper and lowercase, and actually classify characters as well. So there are a number of things we can ask about: like, is it an alpha character, is it alphanumeric, is it alphabetic, is it lowercase? All of these things that start with is, are asking a question and they will return true or false. These last to functions right here on the bottom of the byte character which is what we we're using, has a to lower, and a to upper. So we can take a look at the to lower function, and to see that, what we do is we call it with a character, and it will return that character to us converted to lowercase as appropriate. Don't be confused by the fact that these say int here, we can treat characters as int. As we discovered, when we looked at the char data type, and found that we could set chars to 133, which is the ASCII code for some letter or a character that I don't remember. What I want to do right here, is I want to turn whatever they answered into the lowercase character for whatever they answered. But that means, that up here, I need to pound include ctype.h. So I've done that. So right here, I can say answer equal to lower answer. That might look confusing to you, but remember, assignment is not a statement of mathematical truth. Remember the way assignment statements work is, we evaluate whatever is on the right-hand side of the assignment statement, and that just means we figure out what its value is. Then we set this variable to that value. So this isn't stating mathematical truth, it's saying, whatever this is, put it in here. In this case, what we're saying is, take whatever they responded, make it lowercase, and then put it back into answer. So answer will be either left the same if they entered a lowercase character, or it will be changed to be the lowercase version of whatever uppercase character they entered. Let's run it with a whole bunch of different possibilities. Lowercase y works fine. Uppercase Y works fine. Lowercase n works fine. Uppercase N works fine. Anything wrong says the appropriate error message. To recap, in this lecture, we saw how we can use the different variants of the if statement to implement the selection control structure, to make decisions in our code.