cplusplus-primer-5ed-notes/part_i/chap05_statements.md

137 lines
3.9 KiB
Markdown
Raw Normal View History

2020-10-17 09:25:58 +11:00
# Chapter 5. Statements
## 5.1 Simple Statements
## 5.2 Statement Scope
## 5.3 Conditional Statements
## 5.4 Iterative Statements
### 5.4.1 The `while` Statement
### 5.4.2 Traditional for Statement
### 5.4.3 Range `for` Statement (C++11)
**range for statement**: simpler `for` statement that used to iterate through elements of an container
Syntax:
```cpp
for (declaration : expression)
statement
```
* where *expression* must be a sequence
* *declaration* defines a variable. Every element in sequence must be converted into declaration
* Hence, `auto` type specifier is best
```cpp
vector<int> v = {0,1,2,3,4,5,6,7,8,9};
// range variable must be a reference so we can write to the elements
for (auto &r : v) // for each element in v, as we want to change r, we declare it as reference
r *= 2; // double the value of each element in v
```
which equivalent to
```cpp
for (auto beg = v.begin(), end = v.end(); beg != end; ++beg) {
auto &r = *beg; // r must be a reference so we can change the element
r *= 2; // double the value of each element in v
}
```
Q: Why cannot use a range `for` to add elements to a `vector`
A: In a range `for`, the value of `end()` is cached. If add elements to sequence, the value of `end()` will be invalidated.
### 5.4.4 The do while Statement
## 5.5 Jump Statements
### 5.5.1 The `break` Statement
### 5.5.2 The `continue` Statement
### 5.5.3 The `goto` Statement
???
## 5.6 `try` Blocks and Exception Handling
* **throw expressions**: the part which the detecting part uses to indicate that it encountered sth run-time error
* **try blocks**: code blocks are run, from where exception may occue
* starts with `try` and ends with multiple **catch clause**
* Exceptions thrown from code executed inside a `try` block are handled by one of `cath` clauses.
* `catch` clauses are also called **exception handler**
### 5.6.1 A `throw` Expression
Syntax of `throw` Expression: keyword `throw` followed by an expression.
e.g. convert following to code snippet with `throw`
```cpp
Sales_item item1, item2;
cin >> item1 >> item2;
// first check that item1 and item2 represent the same book
if (item1.isbn() == item2.isbn()) {
cout << item1 + item2 << endl;
return 0; // indicate success
} else {
cerr << "Data must refer to same ISBN"
<< endl;
return -1; // indicate failure
}
```
equivalent with throw expression. Rewrite the test to throw an exception rather than returning an error indicator
```cpp
// first check that the data are for the same item
if (item1.isbn() != item2.isbn())
throw runtime_error("Data must refer to same ISBN");
// if we're still here, the ISBNs are the same
cout << item1 + item2 << endl;
```
* Throwing an exception terminates the current function and transfers control to a handler that know how to handle this error.
* `runtime_error` is one of std library exception types, defined in `stdexcept` header.
### 5.6.2 The `try` Block
general form of `try` block
```cpp
try {
program-statements
} catch (exception-declaration) {
handler-statements
} catch (exception-declaration) {
handler-statements
} // . . .
```
???
### 5.6.3 Standard Exceptions
Exception classed defined in std library, defined in 4 headers:
1. `exception` header: most general exception class.
1. Only show exception occur, no additional info
2. `stdexcept` header: some general-purpose exception classes, as shown in table
3. `new` header: `bad_alloc` exception type, detailed in Chapter 12.1.2
4. `type_info` header: `bad_cast` exception type, detailed in Chapter 19.2
| std exception classes in `<stdexcept>` | Description |
| -------------------------------------- | -------------------------------- |
| `exception` | Most general kind of problem |
| `runtime_error` | Problem detected only at runtime |
| `range_error` | Run-time error: ??? |
??? Not finished ???