C + + Primer Fourth Edition reading notes (v) statements

Source: Internet
Author: User

Typically, statements are executed sequentially. However, in addition to the simplest programs, sequential execution is often not sufficient. To do this, C + + defines a set of control-flow statements that allow for conditional execution or repeated execution of a certain function.

1.1 Simple statements

In C + +, most statements end with semicolons.

An expression statement is used to evaluate an expression.

1.2 Declaration Statements

In C + +, the definition or declaration of an object or class is also a statement.

1.3 Compound statement

A compound statement, commonly called a block, uses a pair of curly braces to enclose a sequence of statements. A block identifies a scope in which the name introduced in the block can only be accessed within the block or nested within the block. Typically, a name is visible only from its definition to the end of the block.

A compound statement is a place where a syntax rule requires that a single statement be used, but the program logic requires more than one statement.

1.5 Statement Scopes

A variable defined in a conditional expression must be initialized, and the condition tests the value of the initialized object.

One of the benefits of restricting the scope of variables defined in a control statement is that they can be reused without worrying about whether their current values are correct each time they are used. For variables that are outside the scope, it is not possible to use their residual values within the scope.

1.6 If statement

The IF statement conditionally executes another statement based on whether a particular expression is true.

1.7 Switch statement

Deeply nested if-else statements are often syntactically correct, but do not logically reflect the programmer's intentions correctly.

Control flow in 1.7.1 switch

There is a common misconception that the program executes only the statements associated with the matching case label. In effect, the program executes from that point and continues executing 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 two case labels have the same value, they can also cause a compile-time error.

1.7.3 switch Internal variable definition

For a switch structure, you can define a variable only after its last case label or the default label.

This rule is developed to avoid the occurrence of code skipping the definition and initialization of variables.

In this case, if you need to define a variable for a particular scenario, you can introduce a block statement that defines the variable in the block statement, ensuring that the variable is defined and initialized before it is used.

1.8 While statement

When the condition is true, the while statement executes the target statement repeatedly.

1.9 For Loop statement

In general, it is used to initialize variables to be modified during each loop, or to assign a starting value.

Use for loops

The following for loop is used to output the contents of a vector object:

for (Vector<string>::size_type IND =0; IND! = Svec.size (); ++ind)

{

cout << Svec[ind];

if (Ind + 1! = Svec.size ())

cout << "";

}

It is calculated in the following order:

(1) Once the loop is started, the init-statement (initialization statement) is executed once. In this example, the IND is defined and initialized to 0;

(2) Next, solve the condition (cyclic condition). If the IND is not equal to Svec.size (), The For loop body is executed, otherwise the loop ends. If the condition is false for 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 omit some parts of the For statement header

For and statements, you can omit either the init-statement (initialization statement), the condition (loop condition), or any one (or all) of expression (expression).

1.8.2 multiple definitions in a For statement header

You can define multiple objects in the init-statement of a for statement, but there can only be one statement at a time, so all objects must have the same generic type.

1.8.3 try blocks and exception handling

The communication between the error detection and error handling sections in the exception mechanism provider. The exception handling in C + + includes:

1, throw expression, the error detection section uses this expression to indicate that an unhandled error has been encountered. It can be said that throw throws an exception condition.

2. Try, the error handling section uses it to handle exceptions. The TRY statement block starts with the Try keyword and ends with one or more catch clauses. The exception that is thrown by code that executes in a try block is usually handled by one of the catch clauses. Because they "handle" the exception, the catch clause is also known as processing code.

3. A set of exception classes defined by the standard library that is used to pass related error information between throw and the corresponding catch.

1.8.3.1 Throw expression

The system throws an exception through the throw expression. A throw expression consists of a keyword throw and a trailing expression, usually ending with a semicolon so that it becomes an expression statement. The type of the throw expression determines the type of the exception being thrown.

1.8.3.2 Try Block

The common syntax for a try block is:

Try

{

Program-statements

}catch (Exception-specifier)

{

Handler-statments

}

catch (Exception-specifier)

{

Handler-statments

}

The try block starts with the keyword try and is followed by a statement sequence block enclosed in curly braces. The try block is followed by one or more catch clauses. Each catch clause consists of three parts: a keyword catch, a single type in parentheses, or a declaration of a single object-called an exception specifier, and a block of statements that is usually enclosed in curly braces. If a catch clause is selected to handle the exception, the associated block statement is executed. Once the catch clause finishes executing, the program process proceeds immediately to execute the statement that follows the last catch clause.

C + + Primer Fourth Edition reading notes (v) statements

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.