true
.
true
. Note that expressions (a) and (b) mean exactly the same thing. Because the operators ==
and <
have higher precedence than &&
, you do not need to include the parentheses. The parentheses do, however, make it easier to read. Most people find the expression in (a) easier to read than the expression in (b), even though they mean the same thing.
true
.
true
.
false
. Since the value of the first subexpression (count == 1)
is false
, you know that the entire expression is false
without bothering to evaluate the second subexpression. Thus, it does not matter what the values of x
and y
are. This is called short-circuit evaluation, which is what C++ does.
true
. Since the value of the first subexpression (count < 10)
is true
, you know that the entire expression is true
without bothering to evaluate the second subexpression. Thus, it does not matter what the values of x
and y
are. This is called short-circuit evaluation, which is what C++ does.
false
. Notice that the expression in (g) includes the expression in (f) as a subexpression. This subexpression is evaluated using short-circuit evaluation as we described for (f). The entire expression in (g) is equivalent to
!( (true || (x < y)) && true )
which in turn is equivalent to !( true && true ), and that is equivalent to !(true), which is equivalent to the final value of false
.
This expression produces an error when it is evaluated because the first subexpression ((limit/count) > 7)
involves a division by zero.
true
. Since the value of the first subexpression (limit < 20)
is true
, you know that the entire expression is true
without bothering to evaluate the second subexpression. Thus, the second subexpression
((limit/count) > 7)
is never evaluated and so the fact that it involves a division by zero is never noticed by the computer. This is short-circuit evaluation, which is what C++ does.
This expression produces an error when it is evaluated because the first subexpression ((limit/count) > 7)
involves a division by zero.
false
. Since the value of the first subexpression (limit < 0)
is false
, you know that the entire expression is false
without bothering to evaluate the second subexpression. Thus, the second subexpression
((limit/count) > 7)
is never evaluated and so the fact that it involves a division by zero is never noticed by the computer. This is short-circuit evaluation, which is what C++ does.
If you think this expression is nonsense, you are correct. The expression has no intuitive meaning, but C++ converts the int
values to bool
values and then evaluates the &&
and !
operations. Thus, C++ will evaluate this mess. Recall that in C++, any nonzero integer converts to true
, and 0
converts to false
. C++ will evaluate
(5 && 7) + (!6)
as follows: In the expression (5 && 7)
, the 5
and 7
convert to true
. true && true
evaluates to true
, which C++ converts to 1
. In (!6)
, the 6
is converted to true
, so !(true) evaluates to false
, which C++ converts to 0
. The entire expression thus evaluates to 1 + 0
, which is 1
. The final value is thus 1
. C++ will convert the number 1
to true
, but the answer has little intuitive meaning as true
; it is perhaps better to just say the answer is 1
.
There is no need to become proficient at evaluating these nonsense expressions, but doing a few will help you to understand why the compiler does not give you an error message when you make the mistake of incorrectly mixing numeric and Boolean operators in a single expression.
2. To this point we have studied branching statements, iteration statements, and function call statements. Examples of branching statements we have studied are if
and if-else
statements. Examples of iteration statements are while
and do-while
statements.
3. The expression 2 < x < 3
is legal. It does not mean (2 < x)&&(x < 3)
as many would wish. It means (2 < x) < 3
. Since (2 < x)
is a Boolean expression, its value is either true
or false
, which converts to 1
or 0
, so that 2 < x < 3
is always true
. The output is “true” regardless of the value of x
.
4. No. The Boolean expression j > 0
is false
(j
was just assigned -1
). The &&
uses short-circuit evaluation, which does not evaluate the second expression if the truth value can be determined from the first expression. The first expression is false
, so the entire expression evaluates to false
without evaluating the second expression. So, there is no division by zero.
Start
Hello from the second if.
End
Start again
End again
large
small
medium
Start
Second Output
End
10. The statements are the same whether the second Boolean expression is (x > 10)
or (x > 100)
. So, the output is the same as in Self-Test Exercise 9.
Start
100
End
12. Both of the following are correct:
if (n < 0)
cout << n << " is less than zero.\n";
else if ((0 <= n) && (n <= 100))
cout << n << " is between 0 and 100 (inclusive).\n";
else if (n >100)
cout << n << " is larger than 100.\n";
and
if (n < 0)
cout << n << " is less than zero.\n";
else if (n <= 100)
cout << n << " is between 0 and 100 (inclusive).\n";
else
cout << n << " is larger than 100.\n";
13. enum
constants are given default values starting at 0
, unless otherwise assigned. The constants increment by 1. The output is 3 2 1 0
.
14. enum
constants are given values as assigned. Unassigned constants increment the previous value by 1. The output is 2 1 7 5
.
15. Roast worms
16. Onion ice cream
17. Chocolate ice cream
Onion ice cream
(This is because there is no break
statement in case 3.)
18. Bon appetit!
19. 42 22
20. It helps to slightly change the code fragment to understand to which declaration each usage resolves.
{
int x1 = 1; // output in this column
cout << x1 << endl; // 1<cr>
{
cout << x1 << endl; // 1<cr>
int x2 = 2;
cout << x2 << endl; // 2<cr>
{
cout << x2 << endl; // 2<cr>
int x3 = 3;
cout << x3 << endl; // 3<cr>
}
cout << x2 << endl; // 2<cr>
}
cout << x1 << endl; // 1<cr>
}
Here <cr> indicates that the output starts a new line.
21. 2 1 0
22. 2 1
23. 1 2 3 4
24. 1 2 3
25. 2 4 6 8
Hello 10
Hello 8
Hello 6
Hello 4
Hello 2
27. 2.000000 1.500000 1.000000 0.500000
A for
loop
and c. Both require a while
loop since the input list might be empty.
A do
-while
loop can be used since at least one test will be performed.
for (int i = 1; i <= 10; i++)
if (i < 5 && i != 2)
cout << 'X';
for (i = 1; i <= 10; i = i + 3)
cout << 'X';
cout << 'X'; //necessary to keep output the same. Note
//also the change in initialization of m
for (long m = 200; m < 1000; m = m + 100)
cout << 'X';
30. The output is 1024 10
. The second number is the base 2 log of the first number.
31. The output is: 1024 1
. The ‘;
’ after the for
is probably a pitfall error.
32. This is an infinite loop. Consider the update expression i = i * 2
. It cannot change i
because its initial value is 0
, so it leaves i
at its initial value, 0
.
33. 4 3 End of Loop
34. 4 3
Notice that since the exit
statement ends the program, the phrase End of Loop
is not output.
35. A break
statement is used to exit a loop (a while
, do-while
, or for
statement) or to terminate a case in a switch
statement. A break
is not legal anywhere else in a C++ program. Note that if the loops are nested, a break
statement only terminates one level of the loop.
for (int count = 1; count <= 10; count++)
cout << "Hello\n";
37. You can use any odd number as a sentinel value.
int sum = 0, next;
cout << "Enter a list of even numbers. Place an\n"
<< "odd number at the end of the list.\n";
cin >> next;
while ((next % 2) = = 0)
{
sum = sum + next;
cin >> next;
}
38. The output is too long to reproduce here. The pattern is as follows:
1 times 10 = 10
1 times 9 = 9
.
.
.
1 times 1 = 1
2 times 10 = 20
2 times 9 = 18
.
.
.
2 times 1 = 2
3 times 10 = 30
.
.
.
39. Tracing a variable means watching a program variable change value while the program is running. This can be done with special debugging facilities or by inserting temporary output statements in the program.
40. Loops that iterate the loop body one too many or one too few times are said to have an off-by-one error.
Off-by-one errors abound in problem solving, not just writing loops. Typical reasoning from those who do not think carefully is
10 posts = 100 feet of fence / 10 feet between posts
This, of course, will leave the last 10 feet of fence without a post. You need 11 posts to provide 10 between-the-post 10-foot intervals to get 100 feet of fence.