C++ Decisions PDF
Document Details
Uploaded by PunctualFoil
Universiti Malaysia Sabah
2012
Cay Horstmann
Tags
Summary
These slides provide an introduction to decision-making in C++ using if statements and switch statements as well as Boolean operators.
Full Transcript
Chapter Three: Decisions C++ for Everyone by Cay Horstmann Slides by Evan Gallagher Copyright © 2012 by John Wiley & Sons. All rights reserved The if Statement The if statement is used to implement a decision. – When a condition i...
Chapter Three: Decisions C++ for Everyone by Cay Horstmann Slides by Evan Gallagher Copyright © 2012 by John Wiley & Sons. All rights reserved The if Statement The if statement is used to implement a decision. – When a condition is fulfilled, one set of statements is executed. – Otherwise, another set of statements is executed. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The if Statement We must write the code to control the elevator. How can we skip the 13th floor? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The if Statement We will model a person choosing a floor by getting input from the user: int floor; cout > floor; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The if Statement If the user inputs 20, the program must set the actual floor to 19. Otherwise, we simply use the supplied floor number. We need to decrement the input only under a certain condition: int actual_floor; if (floor > 13) { actual_floor = floor - 1; } else { actual_floor = floor; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The if Statement C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The if Statement Sometimes, it happens that there is nothing to do in the else branch of the statement. So don’t write it. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The if Statement Here is another way to write this code: We only need to decrement when the floor is greater than 13. We can set actual_floor before testing: int actual_floor = floor; if (floor > 13) { actual_floor--; } // No else needed (And you’ll notice we used the decrement operator this time.) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The if Statement – Always Use Braces When the body of an if statement consists of a single statement, you need not use braces: if (floor > 13) floor--; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The if Statement – Always Use Braces However, it is a good idea to always include the braces: – the braces makes your code easier to read, and – you are less likely to make errors such as … C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The if Statement – Common Error – The Do-nothing Statement Can you see the error? if (floor > 13) ; ERROR { floor--; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The if Statement – Common Error – The Do-nothing Statement if (floor > 13) ; // ERROR ? { floor--; } This is not a compiler error. The compiler does not complain. It interprets this if statement as follows: If floor is greater than 13, execute the do-nothing statement. (semicolon by itself is the do nothing statement) Then after that execute the code enclosed in the braces. Any statements enclosed in the braces are no longer a part of the if statement. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The if Statement – Common Error – The Do-nothing Statement Can you see the error? This one should be easy now! if (floor > 13) { actual_floor = floor - 1; } else ; ERROR { actual_floor = floor; } And it really is an error this time. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Relational Operators C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Relational Operators C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Relational Operators – Some Notes The == operator is initially confusing to beginners. In C++, = already has a meaning, namely assignment The == operator denotes equality testing: floor = 13; // Assign 13 to floor // Test whether floor equals 13 if (floor == 13) You can compare strings as well: if (input == "Quit")... C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Common Error – Confusing = and == The C++ language allows the use of = inside tests. To understand this, we have to go back in time. The creators of C, the predecessor to C++, were very frugal thus C did not have true and false values. Instead, they allowed any numeric value inside a condition with this interpretation: 0 denotes false any non-0 value denotes true. In C++ you should use the bool values true and false C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Common Error – Confusing = and == Furthermore, in C and C++ assignments have values. The value of the assignment expression floor = 13 is 13. These two features conspire to make a horrible pitfall: if (floor = 13) … is legal C++. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Common Error – Confusing = and == The code sets floor to 13, and since that value is not zero, the condition of the if statement is always true. if (floor = 13) … (and it’s really hard to find this error at 3:00am when you’ve been coding for 13 hours straight) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Common Error – Confusing = and == You must remember: Use == inside tests. Use = outside tests. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Multiple Alternatives You use multiple if statements to implement multiple alternatives. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The switch Statement This is a bit of a mess to read. int digit;... if (digit == 1) { digit_name = "one"; } else if (digit == 2) { digit_name = "two"; } else if (digit == 3) { digit_name = "three"; } else if (digit == 4) { digit_name = "four"; } else if (digit == 5) { digit_name = "five"; } else if (digit == 6) { digit_name = "six"; } else if (digit == 7) { digit_name = "seven"; } else if (digit == 8) { digit_name = "eight"; } else if (digit == 9) { digit_name = "nine"; } else { digit_name = ""; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The switch Statement C++ has a statement that helps a bit with the readability of situations like this: The switch statement. ONLY a sequence of if statements that compares a single integer value against several constant alternatives can be implemented as a switch statement. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The switch Statement int digit;. switch (digit) { case 1: digit_name = "one"; break; case 2: digit_name = "two"; break; case 3: digit_name = "three"; break; case 4: digit_name = "four"; break; case 5: digit_name = "five"; break; case 6: digit_name = "six"; break; case 7: digit_name = "seven"; break; case 8: digit_name = "eight"; break; case 9: digit_name = "nine"; break; default: digit_name = ""; break; } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Nested Branches It is possible to have multiple case clauses for a branch: case 1: case 3: case 5: case 7: case 9: odd = true; break; The default: branch is chosen if none of the case clauses match. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Nested Branches Every branch of the switch must be terminated by a break statement. If the break is missing, execution falls through to the next branch, and so on, until finally a break or the end of the switch is reached. In practice, this fall-through behavior is rarely useful, and it is a common cause of errors. If you accidentally forget the break statement, your program compiles but executes unwanted code. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Boolean Variables and Operators Sometimes you need to evaluate a logical condition in one part of a program and use it elsewhere. To store a condition that can be true or false, you use a Boolean variable. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Boolean Variables and Operators In C++, the bool data type represents the Boolean type. Variables of type bool can hold exactly two values, denoted false and true. These values are not strings. There values are definitely not integers; they are special values, just for Boolean variables. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Boolean Variables Here is a definition of a Boolean variable, initialized to false: bool failed = false; It can be set by an intervening statement so that you can use the value later in your program to make a decision: // Only executed if failed has // been set to true if (failed) {... } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Boolean Operators Suppose you need to write a program that processes temperature values, and you want to test whether a given temperature corresponds to liquid water. – At sea level, water freezes at 0 degrees Celsius and boils at 100 degrees. Water is liquid if the temperature is greater than zero and less than 100. This not a simple test condition. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Boolean Operators When you make complex decisions, you often need to combine Boolean values. An operator that combines Boolean conditions is called a Boolean operator. Boolean operators take one or two Boolean values or expressions and combine them into a resultant Boolean value. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The Boolean Operator && (and) In C++, the && operator (called and) yields true only when both conditions are true. if (temp > 0 && temp < 100) { cout