/* This program demonstrates * 1. Shortcut operators * 2. Pre- and post-increment and decrement * 3. Relational operators * 4. Logical operators * 5. The simple if-else statement * Relational Operators * C++ provides us with 6 relational operators: <, <=, >, >=, == and != * Relational operators compare two quantities and return the truth value * of the statement when read left to right. The comparison should be * enclosed in parentheses. * For example, (X < Y) returns "true" if the value in X is less than the * value in Y, and returns "false" otherwise. * X an dy should be comparable. For example, they can both be numeric * quantities, even if X were an int and Y were a float. However, X cannot * be text if Y were a number. * == represents equality. We are asking the computer if the two quantities * are equal. != is the opposite. We ask if the two quantites are not equal. * == is not the same as =. == is comparison, = is assignment. * The relational operators have a precedence just below the math operators. * <, <=, > and >= have higher precedence than == and != * * Logical Operators * C++ provides us with 3 logical operators: && (AND), || (OR) and ! (NOT) * The relational operators can only establish the relationship between 2 * quantities. So, we cannot write things like X < Y < Z. Instead, we * have to combine two relational statements using a logical operator. * If we use the AND operator, the entire expression is true only if ALL the * component relational expressions are true. * If we use the OR operator, the expression is true if AT LEAST ONE of the * component expressions are true. * The NOT operator just negates the truth value of the expression. * * The simple if...else statement. * The if ...else statement is a way to get C++ to execute different lines * of code depending on the truth value of a condition. If the condition * were true, C++ executes the statements in the if block and skips over * the else block. If the condition were false, C++ skips over the if * block and executes the statements in the else block. * Once the if..else statement is done, C++ continues with the first line * after the if...else statement. * Both "if" and "else" are keywords. * 1. Only the if part requires a conditon. NO CONDITION FOR ELSE * 2. The else part is optional, while the if part is not. WE CANNOT WRITE * AN ELSE BLOCK WITHOUT THE IF BLOCK */ #include using namespace std; int main() { /* If we have an assignment of the form * variable = variable operator expression * then, we can rewrite the assignment as * variable op= expression * This is called Shortcutting. * C++ provides us with shortcuts for all 5 binary math operators. */ int val1 = 10; int val2; cout<<"Enter a value for val2: "; cin>>val2; val1 = val1 + (12 * 0.37 - val2 / 19); cout<<"Long form operation: "<< val1 <>x>>y>>z; /* Here, C++ will print 0 for "false" and 1 for "true". * C++ will also translate 0 to "false" and anything not 0 to "true" */ cout<<"Are they equal? "<< (x == y) <= y) < y) < Relational > Logical ( ! > && > ||) * x = 10, y = 12, z = -8 * x>0 && y>0 && z>0 // > associates left to right * 10 > 0 && y>0 && z>0 * true && 12 > 0 && z > 0 * true && true && z>0 * true && true && -8 > 0 * true && true && false // && associates left to righ * true && false * false */ cout<<"Are all 3 positive? "<<(x>0 && y>0 && z>0)< Relational > Logical ( ! > && > ||) * x = 10, y = 12, z = -8 * x < 0 || y<0 || z<0 // < associates left to right * 10 < 0 || y<0 || z<0 * false || 12 < 0 || z < 0 * false || false || z<0 * false || false || -8 < 0 * false || false || true // || associates left to right * false || true * true */ cout<<"At least one negative? "<< (x<0 || y<0 || z<0)< 0 && y > 0 && ! (z > 0 ))<0 && y>0 && z> 0) { cout<<"All 3 numbers are positive"<