Making Decisions

Read time: 15 minutes (3755 words)

See also

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 gate a positive result
  • A >= B is true is the subtraction is positive or zero
  • A < B is true if the subtraction gives a negative result
  • A <= B is true if the subtraction is negative or zero
  • A == B is true only if the subtraction gives zero (Watch this one!)
  • A != B is true only if the subtraction 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:

if(A = B) {
    // do something
}

If that something is just a single statement, we do not even need the curly braces, which surround a sequence of statements:

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.

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:

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!

if(!(A > B)) {
    // do something if A is not greater than B!
}

Using Braces

Although you can get away not using the curly braces in these statements, it is not goog to do so when you are learning. Things can get confusing if you do not use proper style, and those braces. Consider this:

if(A > B)
    cout << "greater" << endl;
    cout << "more processing

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:

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:

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:

if(A > B) {
    min = A;
}
if(A == B) {
    min = B;
}

Is there any difference? 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:

if((A > B) && (A < C)) {
    // do something if between (exclusive)

}

if ((A >= B) && (A <= C)) {
    // do something (inclusive)
}

Watch out here. A common error is this:

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.