CS 107 - 6/22/15 Lecture Quiz 3 Questions // Q 1 int x, y, z; x = 10; y = 5; z = x * 2 + 3 * y; cout << "Q1: The value in z is: " << z << endl; // Q 2 int x, y, z; x = 10; y = 5; z = x + 2 * 3 + y; cout << "Q2: The value in z is: " << z << endl; // Q 3 int x, y, z; x = 6; y = 2; z = x * 2 + 3 * y; x = 3; y = 4; cout << "Q3: The value in z is: " << z << endl; Consider the following assignment statement; int val = 10; val = val + 1; C/C++ have the following short-cut statements val += 1; Is the same as: val = val + 1; ++, -- which increment or decrement a value by 1 val++; is the same as: val = val + 1; val--; is the same as: val = val - 1; Note that these operators can be used in either the prefix format or the postfix format. Prefix: ++val; Postfix: val++; This is getting clever; DON'T DO THIS a = b++ + --c; val = 5; val = val++; // does not produce the intended results The proper code should be: val = 5; val++; Input and Output Standard Input Stream (from the keyboard) is: cin Standard Output Stream (to to terminal) is: cout cout << "Hello There" << endl; The use of cin and cout require the iostream library to be included by the preprocessor directive of: #include To read in a value from the keyboard we use the following: int val; cin >> val; string planet; cin >> planet; If Statements / Selection Statements Chpater 5 The "Flow of Control" so far has been sequential statements When using Selection Statements, the program can "select" whether it will executed a block of statements or not. Boolean Variables/expressions a Boolean variable/expression can have a value of true or false bool finished; finished = false; The above shows the use of Boolean varaibles; however, these are rarely used. The most common way of using boolean expression is through the relational operators == equality (equals) != inequality (not equals) < less than <= less than or equal to > greater than >= greater than or equal to example val >= 10 The above evaluates to either true or false depending on the current value of the variable val The syntax of the simplest form of the if statement is: if ( ) { ; } The statement(s) in the are only executed if the evaulates to true int number1; cin >> number1; // get the absolute value of what the user entered if ( number1 < 0 ) { number1 = 0 - number1 ; } cout << "The absolute value is: " << number1 << endl; The above code without the curly brackets: if ( number1 < 0 ) number1 = 0 - number1 ; When written with out curly brackets, we can accidently introduce errors by adding in extra lines and forgetting to add in the curly brackets. The following shows this error: if ( number1 < 0 ) cout << "Performing the absolute value operation" << endl; number1 = 0 - number1 ; The solution is to add the curly brackets: if ( number1 < 0 ) { cout << "Performing the absolute value operation" << endl; number1 = 0 - number1 ; } Another (more common) version of the if statement is the "if-then-else" statement Syntax: if ( ) { ; } else { ; } If the evaluates to true, the is executed. Otherwise (if the evaluates to false), the is executed. Example: determine the greater of two values int val1, val2; int maxValue; cin >> val1; cin >> val2; if ( val1 >= val2 ) { maxValue = val1; } else { maxValue = val2; } cout << "The larger value is: << maxValue << endl; When we have a larger number of optional blocks of code, we can chain if statements together. Where the next if statement if the of the previous if statement. int average; char grade; cin >> average; if ( average >= 90 ) { grade = 'A'; } else if ( average >= 80 ) { grade = 'B'; } else if ( average >= 70 ) { grade = 'C'; } else if ( average >= 60 ) { grade = 'D'; } else { grade = 'F'; } cout << "The final grade is: " << grade << endl;