CMIS 102 if So far, all the programs we've seen and done have used only sequence as the flow of control. The programs have only used the calculator aspects of the computer: get some input, store it, calculate with it, output it. The computer is differentiated from a calculator by its ability to ask a question and do one thing or the other depending on the answer to the question. In other words, it can make decisions and select one statement or another to execute; it's not limited to executing each and every statement in the order in which they appear in the program. We look now at selection, or branching, control construct. A question or a condition is posed, the answer of which is true or false (there can't and won't be "maybe" answers). Technically, it is an "assertion", but it's easier to think of it as a question. A typical question is asking if the value of variable num is greater than 0, which would be expressed as: num > 0. Selection is done with the "if" statement: if (num > 0) { // 1A here TRUE branch } else { // 1B here FALSE branch } If the answer is true then the TRUE branch is taken and statement 1A is executed, but if the answer is false (i.e. num is less than or equal to 0) then the FALSE branch is taken and statement 1B is executed. One or the other is executed, but not both and not neither. It's like human language and logic. Each of 1A and 1B can consist of any number and kinds of statements. "if" and "else" are reserved words. Every if statement has a parenthesized expression which is the condition. Notice that there is no ; after the condition. The condition is followed by the statement or statements, called the "body", of the if, that are executed if the condition is true. If there is more than one statement in the body, that is called a "compound statement" and must be surrounded by a pair of braces. The body of the if is then [optionally] followed by the else and its body which is executed if the condition is false. An else is never by itself, it always is part of an if statement. Note that the standard math symbols are not available, so C's "relational" operators are composed: <, >, <=, >=, == (equality), != (not equals). Notice that the statements of the body are indented relative to the if and else. That is mandatory style. You have the option of deciding how many spaces you use in a program for the indentation. Your other option: where to put the open brace. Choose one style or the other and use it exclusively in a program (i.e. don't use a variety of styles in a single program because that is distracting and confusing). In the compound statements there is no ; after a }. It wouldn't be a syntax error to put one after the else's closing } but it would be to put one after the if's body's closing } because that would terminate the if and then the else doesn't have an if to match with. Putting a ; after the condition also terminates the if, which creates the same syntax error with the else; but even if there is no optional else there's a problem because the body of the if would always execute, regardless of the value of the condition, because the if in this case doesn't have a body. The else part is optional. A classic error in C++ is to confuse the = with the ==. C++ is not math. = is assignment, == is equality. Try this wrong code: int x, y; cin >> x >> y; if (x = y) //should be == of course cout << "they are equal"; else cout << "they are different"; the condition is an assignment expression, x will be assigned the value of y. The condition is not asking if x and y are equal. In a mileage program, we could check that the start and ending miles are somewhat meaningful by this: cin >> startMiles >> endMiles; if (startMiles > endMiles) cout << "Error! Starting mileage can't be more than ending mileage"; else { //continue with the rest of the program.... } In a triangle program we could "sanity" check the two lengths to make sure they are positive numbers: cin >> lengthOfA >> lengthOfB; if (lengthOfA <= 0) cout << "Error! Length of A must be positive"; if (lengthOfB <= 0) cout << "Error! Length of B must be positive"; The condition can be any expression. Here are some examples using arithmetic and relational operators: if (x <= y/2) ... if (x > sqrt(y)+1) ... if (x/2 < y+z) ... The statements in a body can be anything, including an if statement. "Nested" if statements are common. There's no limit on the level of nesting, it's whatever the problem solution requires. Here's an example: if (divisor != 0) { result = num / divisor; if (result < 0) cout << "negative result"; else cout << "positive result"; } else { result = 0; cout << "Zero divisor!"; } Here's another: if (divisor == 0) { result = 0; cout << "Zero divisor!"; } else { result = num / divisor; if (result < 0) cout << "negative result"; else cout << "positive result"; } Did you notice that these two code fragments are logically identical? They accomplish exactly the same thing. The "dangling else", meaning an else that is after two ifs, seems ambiguous because which if does it match. But it's not a problem because there's a syntax rule that says the else matches the closest preceding unmatched if that is not closed off insides braces. You should be thinking that the level of material seems to have increased in difficulty. It has. We are starting to get into the real meat of programming, which is complexity: the complexity of the real world and thus the complexity of software that models and interacts with the real world. And remember that the computer is a mindless machine that only does what you tell it to do, and you have to be very precise about what you say. That's why programming languages are so detailed. And programming is so hard. Another example of using if statements is to test if opening an input file succeeded or not. The program will be in error if it inputs from a file that didn't open successfully, say if it doesn't exist. So have an if statement that checks if the open worked: ifstream ifs; ifs.open("whatever.huh"); if (!ifs) //test if file stream is not true cout << "Error! File didn't open."; The program could terminate itself by doing a return statement in the body of the if. ??? Very important point about C++: zero means false, non-zero means true. The relational operators evaluate to 1 or 0. 1 is non-zero, therefore "true", 0 is "false". x != y the value of this expression is technically 1 or 0, although we just think of it as true or false.