Exercise 5.1 What is an empty sentence?When do you use empty statements?
An empty statement is a statement that does nothing. You can use an empty statement when you need one syntactically but logically.
Exercise 5.2 What is a block?When will blocks be used?
Blocks are curly bracketed statements and sequences of declarations that can be used when a statement is grammatically required but a set of statements is logically required.
Exercise 5.3 uses a comma operator to rewrite the while loop of section 1.4.1 so that it no longer needs blocks, and to see if the rewritten code readability improves or decreases.
while (val <= 10) sum += val, ++val;
I think the code is really simpler, but it's less readable.
Exercise 5.4 illustrates the meaning of the following example, and if there is a problem, try to modify it.
(a) while (string::iterator iter != s.end()) { /* . . . */ } (b) while (bool status = find(word)) { /* . . . */ } if (!status) { /* . . . */ }
(a) The statement seems to want to iterate through s using an iterator iter, but if the iterator ITER definition is written in the judgement of the while statement, ITER s are redefined for each loop, which is obviously an error.The correct writing should define ITER outside of the while loop.
(b) The while statement and if statement are two code blocks that are independent of each other, so the variable status defined in the while statement cannot be accessed in if. The correct writing should either define the status before the while loop or take the if statement into the while loop to determine.
Exercise 5.5 Write your own program and use if else statements to achieve the requirement of converting numbers to letters.
#include<iostream> #include<vector> #include<string> using namespace std; int main() { int grade; vector<string> souce = { "F", "D", "C", "B", "A", "A++" }; while(cin >> grade){ string lettergrade; if (grade < 60) { lettergrade = souce[0]; } else { lettergrade = souce[(grade - 50) / 10]; } if (grade == 100 || grade < 60) { } else { if (grade % 10 > 7) { lettergrade += "+"; } else if (grade % 10 < 3) { lettergrade += "-"; } } cout << lettergrade << endl; } return 0; }
Exercise 5.6 Rewrite the previous question using conditional operators instead of if else statements.
#include<iostream> #include<vector> #include<string> using namespace std; int main() { int grade; vector<string> souce = { "F", "D", "C", "B", "A", "A++" }; while(cin >> grade){ string lettergrade = grade < 60 ? souce[0] : souce[(grade - 50) / 10]; lettergrade += (grade == 100 || grade < 60) ? "" : (grade % 10 > 7) ? "+" : (grade % 10 < 3) ? "-" : ""; cout << lettergrade << endl; } return 0; }
Exercise 5.7 Rewrite errors in the following code snippets.
(a) if (ival1 != ival2) ival1 = ival2 else ival1 = ival2 = 0; (b) if (ival < minval) minval = ival; occurs = 1; (c) if (int ival = get_value()) cout << "ival = " << ival << endl; if (!ival) cout << "ival = 0\n"; (d) if (ival = 0) ival = get_value();
(a)ival1 = ival2 statement followed by less';'.
(b) minval = ival; and occurs = 1; curly brackets around.
_if(!ival) should be changed to else.
(d)if (ival = 0) should be changed to if (ival = 0).
Exercise 5.8 What is "hanging else"?How does the C++ language handle the else clause?
C++ specifies that else matches the closest unmatched if to it.
Exercise 5.9 Write a program that uses a series of if statements to count how many vowel letters are in the text read from cin.
#include<iostream> using namespace std; int main() { char ch; int sum_a = 0, sum_e = 0, sum_i = 0, sum_o = 0, sum_u = 0; int vowelcnt = 0; while(cin >> ch){ if (ch == 'a') { ++sum_a; ++vowelcnt; } else if(ch == 'e'){ ++sum_e; ++vowelcnt; } else if (ch == 'i') { ++sum_i; ++vowelcnt; } else if (ch == 'o') { ++sum_o; ++vowelcnt; } else if (ch == 'u') { ++sum_u; ++vowelcnt; } } cout << "acnt is: " << sum_a << endl; cout << "ecnt is: " << sum_e << endl; cout << "icnt is: " << sum_i << endl; cout << "ocnt is: " << sum_o << endl; cout << "ucnt is: " << sum_u << endl; cout << "vowelcnt is: " << vowelcnt << endl; return 0; }
Exercise 5.10 There is a problem with the program we previously implemented to count vowel letters: if the vowel letters appear in uppercase, they will not be counted.Write a program that counts both the lowercase and the uppercase vowel letters, that is, the new program should increment the value of aCnt when it encounters'a'and'A', and so on.
#include<iostream> using namespace std; int main() { char ch; int sum_a = 0, sum_e = 0, sum_i = 0, sum_o = 0, sum_u = 0; int vowelcnt = 0; while(cin >> ch){ switch (ch) { case 'a': case 'A': ++sum_a; ++vowelcnt; break; case 'e': case 'E': ++sum_e; ++vowelcnt; break; case 'i': case 'I': ++sum_i; ++vowelcnt; break; case 'o': case 'O': ++sum_o; ++vowelcnt; break; case 'u': case 'U': ++sum_u; ++vowelcnt; break; default: break; } } cout << "acnt is: " << sum_a << endl; cout << "ecnt is: " << sum_e << endl; cout << "icnt is: " << sum_i << endl; cout << "ocnt is: " << sum_o << endl; cout << "ucnt is: " << sum_u << endl; cout << "vowelcnt is: " << vowelcnt << endl; return 0; }