Self-test 2:
Control Flow (If-then-else)

  1. Consider the following two program segments.

    Code segment one:

    int counter = 0;
    while (counter++ < 10)
    {
        cout << counter << " ";
    }
    

    Code segment two:

    int counter = 0;
    while (++counter < 10)
    {
        cout << counter << " ";
    }
    

    What are their outputs? Why?

    Solution

    Code segment one will give "1 2 3 4 5 6 7 8 9 10 " while code segment two will give "1 2 3 4 5 6 7 8 9 ".
    Thus, the output are not the same. In the above codes, counter++ will use the current value of counter for comparison first, and then add 1 to counter.
    On the other hand, ++counter will add 1 to counter first, and use the new value of counter for comparison.
    Therefore, after printing the counter value 9, the logical expression (counter++ < 10) will be (9 < 10) and is evaluated to true, and then counter becomes 10. But when counter is 9, the logical expression (++counter < 10) will become (10 < 10) which is evaluated to false and the loop ends.


  2. Suppose x = 10 and y = 10. What is x after the expression
            (y > 10) && (x++ > 10)
    is evaluated?

    1. 11
    2. 10
    3. 9
    Solution

    B is correct. It is because a logical expression is evaluated from left to right, and the sub-expression (y > 10) is evaluated first. Since the result is false, then (x++ > 10) won't even be evaluated because C++ supports short-circuit evaluation (or lazy evaluation) which means that as soon as the truth value of a logical expression can be decided, any remaining sub-expressions in the logical expression are not evaluated.

    In general:

    Case 1: if ( false && stmt 1 && stmt 2 && .... && stmt N )
    Case 2: if ( true || stmt 1 || stmt 2 || .... || stmt N)
            
    stmt 1, stmt 2,...., stmt N won't be evaluated.
    

    Note: This is NOT mentioned on the lecture slides, and it will not appear in the midterm or final exam.


  3. Analyze the following code:

    Code 1:
    
    bool even;
    if (number % 2 == 0)
        even = true;
    else 
        even = false;
    
    Code 2:
    bool even = (number % 2 == 0);
    
    1. Code 2 has syntax errors.
    2. Both Code 1 and Code 2 are correct.
    3. Both Code 1 and Code 2 perform the same task.
    4. Code 1 has syntax errors.
    Solution

    Both B and C are correct. Recall again that a logical expression has a value which may be directly assigned to a bool variable (c.f. the value of an arithmetic expression may be assigned to a variable. e.g., a = b+c;).


  4. What is the value of n after execution of the following C++ code?

    int n = 22/8;

    Solution

    n = 2. (Integer division)


  5. What is the value of n after execution of the following C++ code?

    int n = 99/8 + 21/11*7;

    Solution

    n = 19. Reason:

    Step1: 99/8 = 12 
    Step2: 21/11*7 = 1*7 = 7
    Step3: 12+7 = 19
    

  6. What is the value of n after execution of the following C++ code?

    int n = 11 + 7/4 + 98%6*3;

    Solution

    n = 18. Reason:

    Step1: 7/4 = 1
    Step2: 98%6 = 2
    Step3: 2*3 = 6
    Step4: 11 + 1 + 6 = 18
    

  7. What is the value of n after execution of the following C++ code?

    int n = 11/31%8*5 - 12;

    Solution

    n = -12. Reason:

    Step1: 11/31 = 0
    Step2: 0%8*5 = 0
    Step3: 0-12 = -12
    

  8. What is the value of n after execution of the following C++ code?

    int n = 1;
    if (n = 0)
        n += 2;
    if (n > 0)
        n *= 2;
    
    Solution

    n = 0. Reason:

    Step1: n = 1
    Step2: n = 0 // n is assigned to 0 here (probably the programmer mistypes == as =)!
    Step3: if (n = 0) means if (0), which is false (but n now has the value of 0), and the body of this if-statement will not be executed. 
    Step4: if (n > 0) means if (0 > 0), which is false, and the body of this if-statement will not be executed.
    Step5: So the final answer is n = 0.
    

  9. What is the value of n after execution of the following C++ code?

    int n = 0;
    int x = 2;
    if (n = (x == 2))
        n++;
    
    Solution

    n = 2. Reason:

    Step1: n = 0, x = 2.
    Step2: x == 2 which is true.
    Step3: "(n = (x == 2))" becomes "(n = true)" which assigns 1 to n (because the internal representation of true is 1).
    Step4: "if (n = true)" becomes "if (true)", which is always satisfied. 
    Step5: n++ is executed because the if-stmt is true, and n becomes 2.
    Step6: So the final answer is n = 2.
    

  10. What is the value of the variable a after execution of the following code?

    #include <iostream>
    using namespace std;
    
    int main()
    {
        int a = 0;
        a = false ? 1 : (true ? 2 : 0);
        return 0;
    }
    
    1. 0
    2. 1
    3. 2
    Solution

    C. It is because the first condition is always false, so it goes to the else-part of the ?: operator. When it encounters the second if-else operator, the condition is always true. As a result, the value of a will be assigned 2.