C ++ Primer fourth edition Reading Notes (5) Statement, primer Fourth Edition
Statements are executed sequentially. However, apart from the simplest program, only sequential execution is often insufficient. Therefore, C ++ defines a set of control flow statements that allow conditional or repeated execution of certain functions.
1.1 simple statements
In C ++, most statements end with a semicolon.
Expression statements are used to calculate expressions.
1.2 statement
In C ++, definitions or declarations of objects or classes are also statements.
1.3 compound statements
Compound statements, usually called blocks, use a sequence of statements enclosed by curly braces. The block identifies a scope. The name introduced in the block can only be accessed within the block or within the Child block nested in the block. Generally, a name is visible only in the range from its definition to the end of the block.
Compound statements are used where syntax rules require a single statement, but the program logic requires more than one statement.
1.5 statement Scope
Variables defined in conditional expressions must be initialized. This condition tests the value of the initialization object.
One advantage of limiting the scope of variables defined in the control statement is that these variable names can be reused without worrying about whether their current values are correct at each usage. For variables outside the scope, it is impossible to use their residual values within the scope.
1.6 if statement
The if statement executes another statement conditionally based on whether the specific expression is true.
1.7 switch statement
Deep nested if-else statements are often syntactically correct, but do not logically reflect the programmer's intent.
1.7.1 control flow in switch
There is a general misunderstanding that the program will only execute the statement associated with the matching case number. In fact, the program starts execution from this point and continues to execute other statements across the case boundary until the switch ends or the break statement is encountered.
1.7.2 switch expressions and case labels
The case label must be an integer constant expression.
If the two case labels have the same value, compilation errors may also occur.
1.7.3 variable definitions in the switch
For the switch structure, variables can only be defined after the last case or default label.
This rule is made to avoid code skipping the definition and initialization of variables.
In this case, if you need to define variables for a special case, you can introduce block statements to define variables in this block statement, this ensures that the variable is defined and initialized before use.
1.8 while statement
When the condition is true, the while statement executes the target statement repeatedly.
1.9 for Loop statements
In general, it is used to initialize the variables to be modified during each cycle, or assign a start value.
Use of for Loop
Assume that the following for loop is used to output the content of the vector object:
For (vector <string >:: size_type ind = 0; ind! = Svec. size (); ++ ind)
{
Cout <svec [ind];
If (ind + 1! = Svec. size ())
Cout <"";
}
The calculation sequence is as follows:
(1) execute init-statement (initialization statement) once at the beginning of the loop ). In this example, ind is defined and initialized to 0;
(2) then, solve the condition (Cyclic condition ). If ind is not equal to svec. size (), execute the for loop body. Otherwise, the loop ends. If the condition is false during the first loop, the for loop body is not executed.
(3) If the condition is true, the for loop body is executed.
(4) Finally, solve the exprission.
1.8.1 omitting some parts of the for statement Header
In for and statements, either init-statement, condition, or expression (expression) can be omitted ).
1.8.2 multiple definitions in the for statement Header
Multiple objects can be defined in the init-statement of the for statement. However, only one statement can appear here, so all objects must have the same general type.
1.8.3 try block and Exception Handling
Communication between error detection and error handling in the exception mechanism provider. C ++ Exception Handling includes:
1. throw expression. The error detection part uses this expression to indicate that an error is not handled. Throw triggers exception conditions.
2. try. The error handling part uses it to handle exceptions. The try statement block starts with the try keyword and ends with one or more catch clauses. The exception thrown by the Code executed in the try block is usually handled by one of the catch clauses. Because they "handle" exceptions, catch clauses are also called processing code.
3. A group of exception classes defined by the standard library are used to pass related error messages between throw and the catch.
1.8.3.1 throw expression
The system throws an exception through the throw expression. The throw expression is composed of the keyword throw and the trailing expression. It usually ends with a semicolon, so it becomes an expression statement. The type of the throw expression determines the type of the thrown exception.
1.8.3.2 try Block
The common syntax format of the try block is:
Try
{
Program-statements
} Catch (exception-specifier)
{
Handler-statments
}
Catch (exception-specifier)
{
Handler-statments
}
The try block starts with the keyword try, followed by the statement sequence block enclosed in curly brackets. The try block is followed by one or more catch clauses. Each catch clause consists of three parts: the keyword catch, the declaration of a single type or single object in parentheses-known as the exception specifier and the statement block usually enclosed in curly brackets. If a catch clause is selected to handle exceptions, the related block statements are executed. Once the execution of the catch clause ends, the program process continues to execute the statement that follows the last catch clause immediately.