#include // FIND THE BUGS // // The following program contains three 'if' statements // involving the logical operators // // && logical AND // || logical OR. // // For each 'if' statement: // // 1. Add parentheses to show how C++ actually // evaluates the expression. // // 2. Describe in words what the expression actually // tests. // // 3. Determine whether this agrees with the message // printed by the 'if' statement. // // 4. If it does not, rewrite the condition so that // it correctly tests the condition described by // the message. // // There is also a coding-style issue. // // In this course, use parentheses to make the operands of // logical operators visually explicit. For example, write // // (a < b) || ((c <= d) && (e > f)) // // rather than // // a < b || c <= d && e > f. // // The first version makes it immediately clear that the // operands of || are // // (a < b) // // and // // ((c <= d) && (e > f)), // // while the operands of && are // // (c <= d) // // and // // (e > f). // // Do not rely on the reader remembering C++ operator // precedence when parentheses can make the intended // logical structure explicit. // Function declarations int main(); // Function definitions int main() { int m{}; int n{}; std::cout << "Enter a first integer: "; std::cin >> m; std::cout << "Enter another integer: "; std::cin >> n; if ( m > 0 && n < -1 || n > 1 ) { std::cout << "Your first integer is positive " << "and your second integer is not " << "one of -1, 0 or 1." << std::endl; } if ( m < 0 || n > 0 && n > m ) { std::cout << "Your second integer is greater " << "than the first, and either the " << "first is negative or the second " << "is positive." << std::endl; } if ( m < 0 || n < 0 && m > 0 || n > 0 ) { std::cout << "One of your integers is negative " << "and the other is positive." << std::endl; } return 0; } // Hint: // // In C++, && has higher precedence than ||. // // Consequently, an expression such as // // A || B && C // // is evaluated as // // A || (B && C), // // not // // (A || B) && C. // // Similarly, // // A && B || C // // is evaluated as // // (A && B) || C. // // Use this rule to add parentheses to each of the three // conditions before trying to fix them. // C++ interprets the first condition as // // if ( ((m > 0) && (n < -1)) || (n > 1) ) { // // Thus, it is true if // // n > 1 // // OR if both // // m > 0 // // and // // n < -1. // // Notice that if n > 1, the condition is true regardless // of the value of m. // // For example, // // m = -100 // n = 2 // // causes the message to be printed even though m is not // positive. // // Does this agree with the message that is printed? // C++ interprets the second condition as // // if ( (m < 0) || ((n > 0) && (n > m)) ) { // // Thus, it is true if // // m < 0 // // OR if both // // n > 0 // // and // // n > m. // // Notice that if m < 0, the condition is true regardless // of whether n is greater than m. // // For example, // // m = -2 // n = -10 // // causes the message to be printed even though n is not // greater than m. // // Does this agree with the message that is printed? // C++ interprets the third condition as // // if ( (m < 0) // || ((n < 0) && (m > 0)) // || (n > 0) ) { // // Thus, it is true if // // m < 0 // // OR if both // // n < 0 // // and // // m > 0 // // OR if // // n > 0. // // Notice that either // // m < 0 // // or // // n > 0 // // is sufficient by itself to make the entire condition // true. // // For example, // // m = -2 // n = -3 // // causes the message to be printed even though both // integers are negative. // // Similarly, // // m = 2 // n = 3 // // causes the message to be printed even though both // integers are positive. // // Does this agree with the message that is printed? // Here are corrected conditions. // // 1. The first integer is positive AND the second integer // is not one of -1, 0 or 1. // // The second integer is outside the interval [-1, 1] // precisely when // // (n < -1) || (n > 1). // // Thus: /* if ( (m > 0) && ((n < -1) || (n > 1)) ) { std::cout << "Your first integer is positive " << "and your second integer is not " << "one of -1, 0 or 1." << std::endl; } */ // 2. The second integer is greater than the first AND // either the first is negative OR the second is // positive. // // Thus: /* if ( (n > m) && ((m < 0) || (n > 0)) ) { std::cout << "Your second integer is greater " << "than the first, and either the " << "first is negative or the second " << "is positive." << std::endl; } */ // 3. One integer is negative AND the other is positive. // // There are two possibilities: // // m < 0 AND n > 0 // // OR // // m > 0 AND n < 0. // // Thus: /* if ( ((m < 0) && (n > 0)) || ((m > 0) && (n < 0)) ) { std::cout << "One of your integers is negative " << "and the other is positive." << std::endl; } */