[C++primer] [06] Statement

Source: Internet
Author: User

6.1 Simple statements

Irrelevant empty statements are not always harmless.

6.2 Declaration Statement 6.3 Compound statement

Also called a block, is a sequence of statements enclosed in a pair of curly braces. A block identifies a scope, and a name is visible only from its definition to the end of the block.

6.4 Statement Scopes

Variables defined in the control structure of a statement are valid only until the end of the block statement that defines them.

6.5 If statement

Whether a class type can be used in a conditional expression depends on the class itself. The IO type can be used as a condition, and vectors and string types are not normally used as conditions.

Pendant else

By default, else matches to the last occurrence of an IF clause that has not yet been matched.

Code style

It is always a good practice to use curly braces after an if statement

6.6 Switch statement using the switch statement

In fact, the program starts with a matching case label and continues executing other statements across the case boundary until the switch ends or the break statement is encountered.

It is best to provide a break statement after each case label.

Allows the program to execute several case labels down, so write:

The case label must be an integer constant expression, or it will compile an error, and it will also result in a compile-time error if the same value is present for the two reference.

If you need to define a variable for a particular case, you can introduce a block statement that defines the variable in the block statement to ensure that the variable is defined and initialized before it is used.

Default Label

Even if there are no statements to execute under the default label, it is still useful to define the default label, which indicates that the situation is considered.

If the switch structure ends with the default label and the default branch does not need to complete any tasks, then the label must be followed by an empty statement.

6.7 While statement

Array content Replication

6.8 For Loop statement
for (initializer; condition; expression)    statemant

The object defined by the For statement header is visible only in the body of the For loop; You can define multiple objects in the initializer of the for statement, but only one statement at a time, so all objects must be of the same type.

6.9 Do While statement
Do
Statementwhile (condition); Note that ending with a semicolon

Any variable that is referenced in a loop condition must already exist before the do statement.

6.10 Break Statement

Break is limited to switch or loop statements that are used to jump out of the inner layer of a switch or loop statement

6.11 Continue statements

Used to bring the most recent loop statement to the end of the iteration, and continue to solve the cyclic condition;

For a For loop, the program flow then solves expression expressions in the For statement.

6.12 Goto Statement

Provides an unconditional jump inside a function and is not recommended for use.

6.13 Try blocks and exception handling

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

1) The throw expression, which is used by the error detection section to indicate that an unhandled error has been encountered and that the throw throws an exception condition;

2) The try block, which is used by the error handling section to handle exceptions. Starts with a try and ends with one or more catch clauses . The exception that is thrown by code executed in a try block is handled by one of the catch clauses, also known as the processing code .

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

Try Block

Below, assume that the standard input check input number cannot be 0

#include <iostream> #include <stdexcept>using namespace Std;int main () {    int val;    cout << "Enter a number (not 0):";    while (Cin >> val) {        try{            if (!val) {                throw runtime_error ("Wrong,it ' s zero!");            }            cout << "OK!" << Endl;        } catch (Runtime_error err) {            cout << err.what ()                << "\ntry Again? Enter y or n "<< Endl;            char c;            CIN >> C;            if (Cin && c== ' n ') break                ;        }    }    return 0;}
function exits when looking for a process to process code

In a complex system, there may be a try nested call, and the process of looking for code is just the opposite of a function call chain. If no corresponding processing code is found, fallback by execution path until the appropriate type of catch is found.

If there is no catch clause to handle the exception, the program jumps to the Terminate function execution, and its execution causes the program to exit abnormally;

An exception that occurs in a program that, if not defined by a try block, is automatically invoked when an exception is found terminate the execution of the terminating program.

Standard Exceptions

6.14 Debugging Ndebug preprocessing variables using a preprocessor

By default, Ndebug is undefined and the program is in the debug state with the following two scenarios:

1) Debug code for development Process Execution

#ifndef ndebugcout << "Debug Info" << Endl; #endif  //#ifndef and #endif之间的代码默认执行

2) assert (ASSERT) macros are defined in the header file Cassert to test for "impossible" conditions

ASSERT (expr);

If the expression is True,assert do nothing, if the expression is False,assert output information and terminate the execution of the program

Both of these debug statements do not execute when you use the-dndebug command-line option to define NDEBUG, or you provide a # define NDEBUG preprocessing command at the beginning of the program.

Several constants defined by the preprocessor

_ _file_ _ Name of the FILE.
_ _line_ _ Current line number.
_ _time_ _ time the file was compiled.
_ _date_ _ DATE the file was compiled.

_ _ func_ _ Name of the function

[C++primer] [06] Statement

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.