D language in the statement (1)

xiaoxiao2021-03-06  41

Statements C and C programmers will find D's statement very familiar, they will find some interesting supplements.

Statement:

Label statement

Statement block

Expression statement

Declaration statement

IF statement

Staff

Version statement

WHILE statement

Dowhile statement

For statement

FOREACH statement

SWITCH statement

CASE statement

DEFAULT statement

Continue statement

BREAK statement

RETURN statement

GOTO statement

WITH statement

SYNCHRONIZE statement

TRY statement

Throw statement

VOLATILE statement

ASM statement

PRAGMA statement

STATEMENT:

LabeledStatement

BlockStatement

ExpressionStatement

DeclarationStatement

IfStatement

DebugStatement

VersionStatement

Whilestatement

DowhileStatement

FORSTATEMENT

ForeachStatement

SwitchStatement

Casestatement

DEFAULTSTATEMENT

Continuestatement

BreakStatement

ReturnStatement

Gotostatement

WITHSTATEMENT

Synchronizestatement

TryStatement

ThrowStatement

VolatileStatement

Asmstatement

Pragmastatement

The label statement statement can be labeled. The label is a marker that follows a statement.

Number statement:

Flags ':' statement

LabelledStatement:

Identifier ':' Statement

Any statement including a null statement can be labeled, so it can be used as a goto statement. The label statement can also be used as the goal of the BREAK or Continue statement.

The label is located in a separate name space, not in the same name space with the declaration, variable, and type, etc. For this, the label cannot be renamed with local declaration. The namespace of the label is the function body they are. The namespace of the label are not nested, that is, the labels in a statement block can be accessed outside of the statement block.

The statement block sequence is a statement sequence that is included by {}. The statement is performed in the order of the words.

Sentence block:

{}

{Statement list}

Statement list:

Statement

Statement statement list

BlockStatement:

{}

{StatementList}

StatementList:

Statement

Statement StatementListListList

The statement block introduces a new scope for local symbols. Despite this, the name of the local symbol must be unique in the function.

Void func1 (int X)

{INT X; // illegal, X is defined multiple times in the function scope

}

Void func2 ()

{

INT X;

{INT X; // illegal, X is defined multiple times in the function scope

}

}

Void func3 ()

{

{INT X;

}

{INT X; // illegal, X is defined multiple times in the function scope

}

}

Void func4 ()

{

{INT X;

}

{x ; // illegal, X is not defined

}

}

The main idea is to avoid bugs caused by the previous name due to the scope declaration within the complex function. All names inside the function must be unique.

Expression statement expressions will be calculated.

Expression statement:

Expression;

ExpressionStatement:

EXPRESSION;

There is no expression, such as (x x), illegal in the expression statement.

Declare statement declaration statement declares and initialize the variable.

Declare statement:

Type logo list;

Signature list:

variable

Variable, logo list

variable:

Marker

Marking = assignment expression

DeclarationStatement:

Type IdentifierList;

IdentifierList:

Variable

Variable, IdentifierList

Variable:

Identifier

Identifier = AssignMENTEXPIPRESSION

If there is no indication

Assigning expression to initialize the variable, the variable will be initialized to its default value.

The IF statement if statement provides a method for performing statements in accordance with the condition.

IF statement:

IF (expression) statement

IF (expression) statement ELSE statement

The expression will be calculated, the result of the calculation must be converted to a Boolean. If the statement after the IF is executed for True, the statement after the ELSE is executed.

The so-called 'suspension ELSE' can be solved by a neighboring matching principle.

The While Statement While statement implements a simple loop structure.

While statement:

While (expression) statement

WhileStatement:

While (Expression) Statement

The expression will be calculated, the result of the calculation must be converted to a Boolean. If you are executed for True

Scriptures. in

After the statement is executed, it will be calculated again.

Expressions, if you are True, execute it again

Scriptures. Continue this process until

The expression results are False.

The BREAK statement will exit the loop. The Continue statement will directly jump to the next calculation expression.

The Do-While Statement Do-While statement implements a simple loop structure.

DO statement:

DO statement while (expression)

DOSTATEMENT:

Do Statement While (Expression)

Expression is first performed. Then calculate

Expression, the result must be converted to a Boolean. If the result is TRUE to continue loop until

The value of the expression is false.

The BREAK statement will launch a loop. The Continue statement will perform the next round of expression valuation.

The FOR statement for statement implements a loop structure with initialization, test, and increment.

FOR statement:

FOR (initialization; test; increment) statement

initialization:

air

expression

statement

test:

air

expression

Increment:

air

expression

FORSTATEMENT:

For (Initialize; Test; Increment) Statement

INITIALIZE:

EMPTY

EXPRESSION

Declaration

Test:

EMPTY

EXPRESSION

Increment:

EMPTY

EXPRESSION

Execute first

Initialization. Then calculate

Test, the result must be converted to a Boolean. If the result is TRUE, execute

Scriptures. Executive

After the statement, will be executed

Increment. Then calculate again

Test, if the result is true to TRUE

Scriptures. Continue this process until

The calculation result of the test is False.

The BREAK statement will launch a loop. The Continue statement will jump directly to the increment.

If the initial value declares a variable, the scope of the variable continues to the end of the statement. E.g:

For (int i = 0; i <10; i )

Foo (i);

Equivalent to:

{INT I;

For (i = 0; i <10; i )

Foo (i);

}

The function body cannot be empty:

For (int i = 0; i <10; i )

; // illegal

The correct way of writing is:

For (int i = 0; i <10; i ) {

}

The initial value can be ignored.

Testing can also be ignored, if ignored, it is assumed to be True.

Foreach statement Foreach statement traverses all the contents of aggregation.

FOREACH statement:

Foreach (Foreach type list; expression) statement

FOREACH Type List:

FOREACH type

Foreach type, foreach type list

FOREACH Type:

INOUT type marker

Type flag

ForeachStatement:

Foreach (ForeachTypelist; Expression) Statement

ForeachTypelist:

ForeachType

ForeachType, ForeachTypelist

FOREACHTYPE:

INOUT TYPE IDENTIFIER

TYPE IDENTIFIER

First calculate

The value of the expression. Its results must be static arrays, dynamic arrays, associated arrays, structural, or class types of aggregated expressions. Perform once for each element of the aggregated expression

Scriptures. At the beginning of each traverse,

The FOREACH Type List declared the variable default to the copy of the aggregated content (ie, using a pass value). If the variable is inout, these variables are references for aggregated content (ie, the use of the way).

If the aggregated expression is a static or dynamic array, one or two variables can be declared. If a variable is declared, this variable is given the value of array elements, one pick one. The type of variable must match the type of the group content, except for the special circumstances mentioned below. If two variables are declared, the first variable corresponds to the index, and the second corresponds to the value. The index must be an int or uint type, which can't be inout, and its value is an index of an array element.

Char [] a;

...

Foreach (int I, char C; a)

{

Printf ("A [% D] = '% C' / N", I, C);

}

If the aggregated expression is a static or dynamic array of CHAR, WCHAR, or DCHAR,

worth it

Type can be char, wchar or dchar

Any one. In this way, any UTF array can be decoded as any UTF type:

Char [] a = "/ xe2 / x89 / xa0"; // / u2260 encoded as 3 UTF-8 BYTE

Foreach (Dchar C; A)

{

Printf ("A [] =% x / n", c); // Print 'a [] = 2260'

}

DCHAR [] B = "/ u2260";

Foreach (char C; b)

{

Printf ("% x,", c); // Print 'E2, 89, A0'

}

If the aggregation expression is an associated array, one or two variables can be declared. If a variable is declared, this variable is given an array element.

Value, one pick one. The type of variable must match the type of the group content. If two variables are declared, the first variable corresponds to

Index, the second corresponds to

Value.

Index must have the same type of index with the associated array, it can't be

INOUT, its value is an index of array elements.

Double [char []] a; // Index Type is char [], the value type is Double

...

Foreach (char [] s, double d; a)

{

Printf ("a ['%. * s'] =% g / n", s, d);

If the aggregated expression is a static or dynamic array, the array element will be traversed from the index 0 to the maximum index. If it is an associated array, the order of the elements is undefined. If it is a structure or a class object, the order of access is

OPApply member function definition.

If the gathering is a structure or class object, it must define an OPApply function with the following type:

INT OPAPPLY (INT DELEGATE (INOUT TYPE [, ...]) DG);

among them

Type wants to declare with the foreach

Marker

Type matching. Pass to

Those remaining in the entrusted type of OPApply

Type ("..." in the above formula corresponding to the rest of the

Foreach type. There are multiple different opApply functions in the structure or class.

The Foreach statement uses a function of type matching, the parameter of the function

DG's parameter type

Corresponding to the foreach statement

Foreach type matches.

OPApply's function is traversed by the gathering elements and transmits them as parameters to

DG function. in case

DG returns 0, then opApply continues to be applied to the next element. in case

DG returns a non-zero value, OPAPPLY must stop traverse and return to that value. If opApply traverses all the elements, it will return 0.

For example, consider containers containing two elements:

Class foo

{

Uint array [2];

INT OPAPPLY (INT DELEGATE (INOUT) DG)

{Int result = 0;

For (int i = 0; i

{

Result = DG (Array [i]);

IF (Result)

Break;

}

Return Result;

}

}

One possible example is:

Void test ()

{

Foo a = new foo ();

A.Array [0] = 73;

A.Array [1] = 82;

Foreach (UINT U; A)

{

Printf ("% d / n", u);

}

}

Will be printed:

73

82

The aggregation can be a string type, such a gathering can be seen as a CHAR, WCHAR, or DCHAR array to access:

Void test ()

{

Foreach (Char C; "ab")

{

Printf ("'% c' / n", c);

}

Foreach (Wchar W; "XY")

{

WPrintf ("'% c' / n", w);

}

}

Will be printed:

'a'

'b'

'x'

'Y'

INOUT is used to update the data item:

Void test ()

{

Static uint [2] a = [7, 8];

Foreach (INOUT uint u; a)

{

U ;

}

Foreach (UINT U; A)

{

Printf ("% d / n", u);

}

}

Will be printed:

8

9

When all data items are traversed in Foreach, you cannot modify the aggregation itself, and you cannot reset the gathering size, reassign, release, reassign or destructive.

Int [] a;

int [] b;

Foreach (INT I; A)

{

A = null; // error a.length = 10; // error

A = b; // error

}

A = null; // ok

If there is in the foreachem

Break statement, you will exit foreach, and

The Continue statement will immediately start the next round of traversal.

转载请注明原文地址:https://www.9cbs.com/read-55572.html

New Post(0)