Self-test 2:
Control Flow (If-then-else)
-
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?
SolutionCode 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.
-
Suppose x = 10 and y = 10. What is x after the expression
(y > 10) && (x++ > 10)
is evaluated?- 11
- 10
- 9
SolutionB 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.
-
Analyze the following code:
Code 1: bool even; if (number % 2 == 0) even = true; else even = false;Code 2: bool even = (number % 2 == 0);
- Code 2 has syntax errors.
- Both Code 1 and Code 2 are correct.
- Both Code 1 and Code 2 perform the same task.
- Code 1 has syntax errors.
SolutionBoth 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;).
-
What is the value of n after execution of the following C++ code?
int n = 22/8;
Solutionn = 2. (Integer division)
-
What is the value of n after execution of the following C++ code?
int n = 99/8 + 21/11*7;
Solutionn = 19. Reason:
Step1: 99/8 = 12 Step2: 21/11*7 = 1*7 = 7 Step3: 12+7 = 19
-
What is the value of n after execution of the following C++ code?
int n = 11 + 7/4 + 98%6*3;
Solutionn = 18. Reason:
Step1: 7/4 = 1 Step2: 98%6 = 2 Step3: 2*3 = 6 Step4: 11 + 1 + 6 = 18
-
What is the value of n after execution of the following C++ code?
int n = 11/31%8*5 - 12;
Solutionn = -12. Reason:
Step1: 11/31 = 0 Step2: 0%8*5 = 0 Step3: 0-12 = -12
-
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;Solutionn = 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.
-
What is the value of n after execution of the following C++ code?
int n = 0; int x = 2; if (n = (x == 2)) n++;Solutionn = 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.
-
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; }- 0
- 1
- 2
SolutionC. 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.