2.1, New concept:
1.) while statement
2.) while body
3.) block
4.) condition
5.) inequality operator
6.) bool
7.) true
8.) false
9.) loop invariant
10.) unsigned
11.) if statement
12.) equality
13.) logical-or
14.) precedence
15.) short-circuit evaluation
16.) logical-and
17.) compound-assignment
18.) using-declaration
19.) off-the-end value
20.) half-open range
21.) for statement
22.) for header
23.) for body
24.) usual arithmetic conversions
2.2, Sample code:
1 #include2 #include 3 4 // say what standard-library names we use 5 using std::cin; using std::endl; 6 using std::cout; using std::string; 7 8 int main() 9 {10 // ask for the person's name11 cout << "Please enter your first name: ";12 13 // read the name14 string name;15 cin >> name;16 17 // build the message that we intend to write18 const string greeting = "Hello, " + name + "!";19 20 // the number of blanks surrounding the greeting21 const int pad = 1;22 23 // the number of rows and columns to write24 const int rows = pad * 2 + 3;25 const string::size_type cols = greeting.size() + pad * 2 + 2;26 27 // write a blank line to separate the output from the input28 cout << endl;29 30 // write rows rows of output31 // invariant: we have written r rows so far32 for (int r = 0; r != rows; ++r) {33 34 string::size_type c = 0;35 36 // invariant: we have written c characters so far in the current row37 while (c != cols) {38 39 // is it time to write the greeting?40 if (r == pad + 1 && c == pad + 1) {41 cout << greeting;42 c += greeting.size();43 } else {44 45 // are we on the border?46 if (r == 0 || r == rows - 1 ||47 c == 0 || c == cols - 1)48 cout << "*";49 else50 cout << " ";51 ++c;52 }53 }54 55 cout << endl;56 }57 58 return 0;59 }
2.3, Important notes:
1.) In line of 25, we use conststring::size_type cols
Whenever we need a local variable to contain the size of a string, we should use std::string::size_type as the type of that variable.
The reason that we have given cols a type of std::string::size_type is to ensure that cols is capable of containing the number of characters in greeting, no matter how large that number might be.
Accordingly, std::string::size_type is an unsigned type-objects of that type are incapable of containing negative values.
2.) If a program finds that the left operand of || is true, it does not evaluate the right operand at all. This property is often called short-circuit evaluation.
3.) C++ programs can extend the core language by defining what it means to apply built-in operators to objects of class type.
4.) Most of the operators are left-associative, although the assignment operators and the operators taking a single argument are right-associative.
5.) We've ordered them by precedence from highest to lowest:
- x.y The member y of object x
- x[y] The element in object x indexed by y
- x++ Increments x, returning the original value of x
- x-- Decrements x, returning the original value of x
- ================================================================
- ++x Increments x, returning the incremented value
- --x Decrements x, returning the decremented value
- !x Logical negation. If x is true then !x is false.
- ================================================================
- x * y Product of x and y
- x / y Quotient of x and y. If both operands are integers, the implementation chooses whether to round toward zero or - ∞
- x % y Remainder of x divided by y, equivalent to x - ((x / y) * y)
- ================================================================
- x + y Sum of x and y
- x - y Result of subtracting y from x
- ================================================================
- x >> y For integral x and y, x shifted right by y bits; y must be non-negative.If x is an istream, reads from x into y
- x << y For integral x and y, x shifted left by y bits; y must be non-negative.If x is an ostream, writes y onto x.
- ================================================================
- x relop y Relational operators yield a bool indicating the truth of the relation.The operators (<, >, <=, and >=) have their obvious meanings.
- ================================================================
- x == y Yields a bool indicating whether x equals y
- x != y Yields a bool indicating whether x is not equal to y
- ================================================================
- x && y Yields a bool indicating whether both x and y are true.Evaluates y only if x is true.
- ================================================================
- x || y Yields a bool indicating whether either x or y is true.Evaluates y only if x is false.
- ================================================================
- x = y Assign the value of y to x, yielding x as its result.
- x op= y Compound assignment operators; equivalent to x = x op y,where op is an arithmetic or shift operator.
- ================================================================
- x ? y1 : y2 Yields y1 if x is true; y2 otherwise.Evaluates only one of y1 and y2.
- ================================================================
6.) size_t: Unsigned integral type (from <cstddef>) that can hold any object's size.
7.) Statements:
using namespace-name::name; Defines name as a synonym for namespace-name::name.
type-name name; Defines name with type type-name.
type-name name = value; Defines name with type type-name initialized as a copy of value.
type-name name(args); Defines name with type type-name constructed as appropriate for the given arguments in args.
expression; Executes expression for its side effects.
{ statement(s) } Called a block. Executes the sequence of zero or more statement(s) in order. May be used wherever a statement is expected. Variables defined inside the braces have scope limited to the block.
while (condition) statement If condition is false, do nothing; otherwise, execute statement and then repeat the entire while.for(init-statement condition; expression) statement Equivalent to { init-statement while (condition) {statement expression; } }.
if (condition) statement Executes statement if condition is true.
if (condition) statement else statement2 Executes statement if condition is true, otherwise executes statement2. Each else is associated with the nearest matching if.
return val; Exits the function and returns val to its caller.