##Exercise 5.1
What is a null statement? When might you use a null statement?
null statement is the empty statement. like this:
; // null statement
I might use a null statement when the **language requires a statement but the program's logic does not. For example:
// read until we hit end-of-file or find an input equal to sought
while (cin >> s && s != sought)
; // null statement.
##Exercise 5.2
What is a block? When might you might use a block?
block is a (possiby empty) sequence of statements and declarations surrounded by a pair of curly braces.
I might use a block when the language requires a single statement but the logic of our program needs more than one. For example:
while (val <= 10)
{
sum += val;
++val;
}
##Exercise 5.3
Use the comma operator (§ 4.10, p. 157) to rewrite the while loop from § 1.4.1 (p. 11) so that it no longer requires a block. Explain whether this rewrite improves or diminishes the readability of this code.
#include <iostream>
int main()
{
int sum = 0, val = 1;
while (val <= 10)
sum += val, ++val;
std::cout << "Sum of 1 to 10 inclusive is "
<< sum << std::endl;
return 0;
}
This rewrite diminishes the readability of the code. The comma operator always guarantees the order and discards the front result. But there are no meaning in this example, however, also are incomprehensible.
##Exercise 5.4
Explain each of the following examples, and correct anyproblems you detect.
- (a) while (string::iterator iter != s.end()) { /* . . . */ }
- (b) while (bool status = find(word)) { /* . . . / } if (!status) { / . . . */ }
(a) iter point at nothing. invalid.
std::string::iterator iter = s.begin();
while (iter != s.end()) { /* . . . */ }
(b) The if statement is not in the while's block. so the status
is invalid. And if status is true, go through the while
block forever, else go through the if
block.
if (!find(word)) { /* . . . */ }
else while (true) { /* . . . */ }