Home  >  Article  >  Backend Development  >  Chapter 5 C++: Introduction to statements

Chapter 5 C++: Introduction to statements

php是最好的语言
php是最好的语言Original
2018-08-06 10:43:311805browse

Chapter 5 Statement

##Hanging else

refers to how to find the else when the if branch is hiding from the else branch Problem with the if branch that matches:

- C specifies that else matches the nearest if that hasn't been matched yet, thus removing the ambiguity.

switch

switch (ch) {    case 'a':    case 'b':
        ++n;        break;
}

  • break jumps out of the switch control flow.

  • case label (case label) must be

    integer constant expression.

  • If a case label is matched successfully, all branches will be executed sequentially starting from the label. Unless the process is explicitly interrupted, it will not stop until the end of the switch. .

  • Add curly braces ({ }) to the statement after the case tag to define the variable within the block, thereby ensuring that all subsequent case tags are outside the scope of the variable.

  • default tag: a special case tag. When switch cannot match any case, the content under the default tag will be executed.

do while

do
    statementwhile (condition);  //注意分号

  • Do not allow variables to be defined within loop conditions.

  • And the variables in the conditional part must be defined outside the loop.

Jump statement

  • break

    is responsible for terminating the while, do while, for or switch statement closest to it. And execution starts from the first statement after these statements.

  • continue

    for, while, do while

  • ##try statement block and exception handling

  • throw expression

    (throw expression): The anomaly detection part uses throw expressions to indicate that it has encountered a problem that cannot be handled.

    • It is said that throw** raises an exception.
    • After an exception is thrown, the function will be terminated and control will be transferred to code that can handle the exception.
  • try statement block

    (try block):try starts with one or more catch clauses(catch clause) Finish.

    • #Exceptions thrown by the code in the try statement block are usually handled by a catch clause.
    • Call the catch clause
    • as the exception handling code (exception handler)

      .

    Exception class
  • try {
        //program-statements
    } catch (/*exception-declaration异常声明*/) {
        //handler-statements
    } catch (/*exception-declaration*/) {
        //handler-statements
    } //...
  • Exception declaration

    Specify the Exception types that the catch clause can handle.

  • Standard Exceptions

C The standard library defines a set of classes for reporting problems encountered by standard library functions.

In the following four header files:

- exception: defines the most common exception class exception, which only reports the occurrence of exceptions and does not provide any additional information.
- stdexcept
- Use string objects or C-style string initialization
- There is only one member function what()


Exception //The most common problems runtime_error //Problems that can only be detected at runtime <br> overflow_error // <br> Underflow_error <br> Logic_error <br> domain_error <br> invalid_arguement <br> length_error <br> out_og_range <br><br> <br>- new: bad_alloc - type_info: bad_cast

Reference: C Primer 5th Edition

Related articles:

Chapter 4 C: Expression Concept - Application of Operators

Chapter 6 C: Function Basics and Application

The above is the detailed content of Chapter 5 C++: Introduction to statements. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn