C + + Primer 5th notes: Fifth Chapter

Source: Internet
Author: User

Fifth Chapter: Statement

Notes

1. Compound statements are sequences of statements and declarations enclosed in curly braces, and compound statements are called blocks. A block is a scope .

2. Variables defined in the control structure (if, switch, while, for) are visible only within the corresponding statement, and once the statement is finished, the variable is beyond its scope.

3. For the overhang else problem, C + + stipulates that else matches the not-so-recent if match, which eliminates the two semantics of the program.

4. The case label must be an integer constant expression in the switch statement, and the value of any two case labels cannot be the same .

5. The C + + language specifies that an initialization statement that crosses a variable is not allowed to jump directly to another location in the scope of the variable (p163). If you need to define and initialize a variable for a case branch, we should define the variable inside the block , ensuring that all subsequent case labels are outside the scope of the variable.

6. keep in mind that the objects defined in the For statement are visible only within the For loop body .

7. The break statement is responsible for terminating the most recent while, do, and for or switch statements from it, and continues execution from the first statement following these statements.

8. The continue statement terminates the current iteration in the most recent loop and immediately begins the next iteration. The continue statement can only appear inside the for, while, and do while loops. ( note : no switch)

Summary of key points of knowledge:

    A. scope for statement:

The new C++11 Standard introduces a simpler for statement that can traverse all elements of a container or other sequence. Form:

 for (declaration:expression)   statement

Expression must be a sequence , such as an initial list of values enclosed in curly braces , an array, a vector, or a string of objects of a type that have the common feature of having a begin () and an end that can return an iterator ( Members declaration defines a variable in which each element of the sequence can be converted to the type of the variable, and the simplest way to ensure type compatibility is to use the auto type specifier . If you need to perform write operations on elements in a sequence, the loop variable must be declared as a reference type .

Each iteration defines a loop control variable, initializes it to the next value in the sequence, and then executes the statement.

vector<int> v = {01234567 8 9    }; the //  range variable must be a reference type in order to perform write operations on the element for  (Auto &r:v)    2 ;            // double the value of each element in V    

    Two. Try statement blocks and exception handling

Exceptions are anomalous behaviors that exist at run time that are outside the scope of the function's normal function.      Typical exceptions include losing database connections , encountering unexpected inputs , and so on.

Exception handling is required when a part of the program detects a problem that it cannot handle. At this point, the part that detects the problem should send a signal to indicate that the program is in trouble and cannot continue, and that the sender of the signal does not need to know where the fault will be resolved.

1#include <stdexcept>2#include"sales_item.h"3#include <iostream>4 usingstd::cin;5 usingStd::endl;6 usingstd::cout;7 usingStd::runtime_error;//Runtime_error is an exception class8 9 intMain ()Ten { One Sales_item item1, item2; A      while(Cin >> Item1 >>item2) -     { -         Try                  //exceptions that are thrown by code in a try statement block are usually handled by a catch clause the         { -             if(ITEM1.ISBN () = =ITEM2.ISBN ()) -             { -cout << item1 + item2 <<Endl; +             } -             Else +             { A                 ThrowRuntime_error ("Date must refer to same ISBN");  at                                 //throw throws (raise) an exception -             } -         } -         Catch(Runtime_error Err)//Runtime_error Class-specific information about passing exceptions -         { -cout <<err.what () in<<"\ n Try Again? Enter y or N"<<Endl; -             CharC; toCIN >>C; +             if(!cin | | c = ='N') -                  Break; the         } *     } $     return 0;Panax Notoginseng}

The C + + standard library defines a set of classes that report problems encountered by standard library functions and are defined in four header files, respectively:

Exception, Stdexcept, new, Type_info

Terms

Expression statement, compound statement (compound statement), dangling else (dangling else),

The range for statement (rang for statement), the throw expression (throw expressions), the TRY statement block (try block), and the Exception class (Exception Class).

C + + Primer 5th notes: Fifth Chapter

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.