.. _lecture5: Making Decisions ################ .. wordcount:: .. vim:ft=rst spell: .. seealso:: Text: Chapter 4 We must ask questions about the values our data have in most programs. These questions must always be in the form that results in an answer of true or false only. (No maybe allowed). We use these questions to control the processing of our data. We will do one thing if the answer it "true" and do something different if the answer is "false". The exact form of the question is called a "logical expression". Logical operators ***************** We ask basic questions by evaluating the relationship between two numbers. How this is done is actually very simple. If we want to compare two values, `A` and `B`, all we need to do is subtract them and see what happens. There are only three possibilities: * A - B is exactly zero, the values were equal * A - B is greater than zero, A is bugger than B * A - B is less than zero, A is less than B So the relational operators just do this simple subtraction, and check the sign of the result, and if that result was zero Relational expressions work like this: * A > B is true if the subtraction gave a positive result * A >= B is true is the subtraction gave positive or zero result * A < B is true if the subtraction gave a negative result * A <= B is true if the subtraction gave a negative or zero result * A == B is true only if the subtraction gives zero (Watch this one!) * A != B is true only if the subtraction gives a result that is not zero Cool! The C++ View of Truth ********************* C/C++ has a strange notion about true and false values. C++ supports the keywords ``true`` and ``false``, but it alsoo accepts integer numbers where a logical result is needed. In C++ a value of zero is ``false`` and anything else is ``true``. Strange! I recommend not creating such code, as it can confuse the reader (and yourself!) The IF Statement **************** Suppose you wnt to do something `only if` two variables have the same value. Here is a simple ``if statement`` that will do the job: .. code-block:: c if(A == B) { // do something } If that something is just a single statement, we do not even need the curly braces, which nirmally surround a sequence of statements: .. code-block:: c if(A == B) cout << "they are equal" < endl; We just place the statement next (on the same line if it is short enough!) or n the next line, indented to show it is "inside" the ``if statement``. If the statement is short, place it on the sam eline with the "if". If it is too long, put it on the next ine, but indent it properly!. Just because you gave up on Python is no reason to forget your good style manners! The IF-THEN-ELSE Statement ************************** In that last statement, we only do something if the "condition" (the relational expression inside the parentheses) evaluates to true. If we want to do something else if it evaluates to ``false``, we add an "else" clause: .. code-block:: c if(A > B) { // do something if true } else { // do something else if false } You cannot just do something if the expression evaluates to false, but you can ask the question in a different way! .. code-block:: c if(!(A > B)) { // do something if A is not greater than B! } .. warning:: Make sure you know what the opposite of each logical operator reall is! Using Braces ************ Although you can get away not using the curly braces in these statements, it is not good to do so when you are learning. Things can get confusing if you do not use proper style, and those braces. Consider this: .. code-block:: c if(A > B) std::cout << "greater" << std::endl; std::cout << "more processing" << std::endl; This is your Python self getting you into trouble. Just because you indented the second line below the if does not make it part of that true part processing. With no curly braces, the ``if`` applies to only that first statement. Nested IF statements Suppose you want to ask a more complex question: .. code-block:: c if(A > B) { min = B; } else { if(A == B) { min = A; } } That second test is only done if the first test results in ``true``. If we have more questions to ask, this gets messy. A better way to write this is this: .. code-block:: c if(A > B) { min = B; } else if(A == B) { min - A; } This allows more questions, and we do not indent ourselves off the right side of the screen! What about this code: .. code-block:: c if(A > B) { min = B; } if(A <= B) { min = A; } Is there anyithing wrong here? Actually, yes! In this last example, we are going to ask the two questions every time, even if the answer to the first question is ``true``. In that case, we know the answer to the second question is ``false`` but we ask it anyway. It is still correct, only slower! More Complex Questions ********************** If your questions get more complicated, you can use nested IF statements, or create a different kind of logical expression using "logical conjunctions". A common problem asks if a number is between two values: .. code-block:: c if((A > B) && (A < C)) { // do something if between (exclusive) } if ((A >= B) && (A <= C)) { // do something (inclusive) } That "&&" is the logical "and" operator you used in Python. The logical "or" in C++ is written "||". Guido hated those! Watch out here. A common error is this: .. code-block:: c if(A > B) || (A < C)) { // do something no matter what! What? Think abut it! } We will show more examples of decisions and logical operators as the need arises. For now, read through Chapter 4 fo more examples of using these expressions and statements.