Switch statement Switch statement selection one CASE statement is executed in accordance with the value of the Switch expression.
Switch statement:
Switch statement block
CASE statement:
Case expression list: statement
DEFAULT statement:
DEFAULT: statement
SwitchStatement:
Switch (Expression) BlockStatement
CaseStatement:
Case ExpressionList: Statement
DEFAULTSTATEMENT:
Default: Statement
Expression is calculated. The type T of the result must be an integer type or char [] or wchar []. The resulting results were compared to each CASE expression. If there is a match, turn the corresponding CASE statement.
The CASE expression is also a list of expressions, which is a list of expressions separated by commas.
If there is no CASE expression to match, and there is a default statement, turn around the DEFAULT statement.
If there is no CASE expression to match, there is no default statement, it will throw the Switcherror exception. The reason why this is done, in order to capture some common mistakes, such as adding a new value for enumeration, but I forgot to provide the corresponding CASE statement for this value. This is not only the same as C and C .
The CASE expression must be a constant value or an array, and must be implicitly converted to the type T of the Switch expression.
The value of the CASE expression must be different from each other. Can't have more than one default statement.
The CASE statement and default statements associated with Switch can be nested in the statement block; they don't have to be in the outermost block. For example, the following is legal:
Switch (i)
{
Case 1:
{
Case 2:
}
Break;
}
As in C and C , the case statement will sequentially perform the following Case clause. BREAK statement will exit Switch
Statement block. E.g:
Switch (i)
{
Case 1:
X = 3;
Case 2:
X = 4;
Break;
Case 3, 4, 5:
X = 5;
Break;
}
If i is equal to 1, X will be equal to 4.
Note: Unlike C and C , strings can be used in Switch Expressions. E.g:
CHAR [] Name;
...
Switch (name)
{
Case "fred":
Case "Sally":
...
}
For such syntax, the code is done directly, clearer and less error. Both ASCII and Wchar can be used.
Realizing the code generation program of the compiler that the CASE statement has been sorted in accordance with the frequency of use, the frequency is high, and the frequency is low in the last. Although this does not affect the correctness of the program, it is beneficial for improved performance.
Continue statement Continue This iteration of the loop statement it is aborted and directly starts the next iteration.
CONTINUE statement:
CONTINUE;
Continue flag;
ContinuestTatement:
CONTINUE;
CONTINUE Identifier;
Continue executes the next iteration of the innermost while, for or DO statement it.
If the Continue i is following the flag, the flag must be the label outside the While, For or DO statement, and Continue executes the next iteration of the loop. If there is no such statement, it is an error. All skipped Finally clauses are executed, and all skip synchronization objects will be released. (Translation: Not currently implemented.)
Note: If there is Return, Throw or Goto to Finally clauses, the target of Continue can never be reached.
BREAK statement BREAK exits its statement.
BREAK statement:
Break;
Break marker;
BreakStatement:
Break;
Break Identifier;
Break exits it the innermost While, for, DO or Switch statement, and resumes execution after this statement.
If Break is following the flag, the flag must be the label it is on the While, For, Do or Switch statement, and Break exits the statement. If there is no such statement, it is an error.
All skipped Finally clauses are executed, and all skip synchronization objects will be released. (Translation: Not currently implemented.)
Note: If there is Return, Throw, or goto to the finally clause, Break's goal will never be reached.
The role of the RETURN statement RETURN statement is to introduce the current function and provide a return value.
RETURN statement:
Return;
Return expression;
ReturnStatement:
Return;
Return Expression;
If the function specifies the type of non-VOID, you must give
Expression.
The expression will be implicitly converted to the return type of the function.
If the function specifies the type of non-VOID, at least one Return statement must be provided.
Even if the return of the function is Void, there is also an expression. The expression will be calculated, but nothing will not return.
All peripheral FinalLy clauses are executed before the function is actually returned, and all the synchronization objects of the periphery will be released.
If there is Return, Goto or Throw in the peripheral Finally clause, the function will not return normally.
If there is a post-test condition (see Contract programming), then the post-test condition is performed after calculating the expression, then the function returns.
GOTO statement goto will jump to
The flag is running at the statement of the label.
GOTO statement:
Goto flag;
Goto Default;
Goto Case;
Goto case expression;
Gotostatement:
Goto Identifier;
Goto Default;
Goto Case;
Goto Case Expression;
Second form,
Goto Default;, you will jump to the innermost layer
Switch statement
DEFAULT statement.
The third form, goto case; will jump to the next CASE statement in the inner Switch statement.
The fourth form, goto case expression; will jump to the CASE statement in the innermost Switch statement containing the expression of the expression.
All skipped Finally clauses are executed while all skipped synchronous muters are released. (Translation: Not currently implemented.)
Using GOTO to skip initialization is illegal. WITH statement with statement is used to simplify repetitive references to the same object.
WITH statement:
WITH state block
With (template instance) statement block
WITHSTATEMENT:
With (Expression) BlockStatement
With (TemplateInstance) BlockStatement
The result of the expression is a reference to class instances or structures. In the WITH process, all flag symbols will be found in the namespaces referenced by the class. WITH statement:
With (Expression)
{
...
Ident;
}
Equivalent in semantics:
{
Object TMP;
TMP = Expression;
...
TMP.IDENT;
}
Note that the expression will only be calculated once. WITH statement does not change
THIS or
SUPER references.
SYNCHRONIZE statement SYNCHRONIZE The statement is used to synchronize the statements in the critical region in the multi-thread.
SYNCHRONIZE statement:
SYNCHRONIZED statement
SYNCHRONIZED (expression) statement
SYNCHRONIZESTATEMENT:
Synchronized Statement
Synchronized (Expression) Statement
Synchronized only allows only one thread to execute each time
Scriptures.
In this usage in Synchronized (Expressions), the result of the expression should be a reference to an Object object, which only allows only the Object execution statement in a thread at a time.
Synchronize when the statement encounters an exception, goto or returnires.
Example:
SYNCHRONIZED {...}
This implements a standard critical area.
The TRY statement is complete by the try-catch-finally statement.
TRY statement:
TRY sentence block multiple caratch
TRY sentence block catchs finally sentence block
TRY statement block Finally sentence block
Multiple Catch:
Lastcatch
Catch
Catch multiple caratch
Lastcatch:
Catch statement block
Catch:
Catch (parameter) statement block
TryStatement:
Try BlockStateChes
Try BlockStatement Catches Finally BlockStatement
Try BlockState Finally BlockStatement
Catches:
Lastcatch
Catch
Catch catches
Lastcatch:
Catch BlockStatement
Catch:
Catch (parameter) blockStatement
Parameter declares the type T variable V, where t is Object or derived from Object. If T is the same as the THROW expression type or the base class of the throw expression, V is initialized to the Throw expression. If the abnormal object is a T type or from T derive, the catch clause will be executed.
If only type T is given, the variable V is given, the catch clause will still be executed.
If there is a Catch parameter that is T1, the Catch of the latter type T2 is masked, that is, if T1 is the same as the T2 type or the base class of T2, it is an error.
LastCatch captures all exceptions.
The Throw statement throws an exception.
THROW statement:
THROW expression;
ThrowStatement:
Throw Expression;
The expression is calculated, and the result must be the object reference type. The reference to the object is thrown as an abnormality. The volatile statement does not have a code activity that can span the boundaries of the Volatile statement.
VOLATILE statement:
VOLATILE statement
VolatileStatement:
Volatile Statement
The statement is calculated. in
All memory write operations before the statement are guaranteed
The statement is completed before or after the memory read operation. in
All memory readings after the statement are guaranteed
After all of the statements or all the memory written in their previous memory are completed.
The volatile statement cannot guarantee atomicity. If you want to guarantee atomicity, you should use the SYNCHRONIZED statement.
ASM statement ASM statement is used to support inline assembly:
ASM statement:
ASM {}
ASM {ASM instruction list}
ASM instruction list:
ASM directive;
ASM instruction; ASM instruction list
AsmStatement:
ASM {}
ASM {asminstructionList}
AsminstructionList:
Asminstruction;
Asminstruction; asminStructionList
The ASM statement allows you to directly use assembly language instructions. This does not have to use external assembler to obtain direct access capabilities for special features of CPU. The D compiler will manage your function to call agglomeration, stack settings, etc.
The format of the instruction, of course, highly dependent on the intent set of the target CPU, is defined by implementation. However, the format needs to follow the following conventions:
You must use the same number of marks as D language. The form of the annotation must be in communication with D language. The ASM instruction is ending in ';', not ending at the end of the row. These rules ensure that the source code of D can be marked independently of syntax or semantic analysis.
For example, for Intel Pentium processors:
INT x = 3;
ASM
{
MOV EAX, X; // load X load register EAX
}
Inline assembly can be used to access hardware:
INT gethardware ()
{
ASM
{
MOV EAX, DWORD PTR 0x1234;
}
}
For some D implementation, if D translate is a translation of the translator, the inline assembly does not make sense, and it is not necessary to implement it. The Version statement can be used to cope with this situation:
Version (InlineAasem)
{
ASM
{
...
}
}
Else
{
... do not use the inline assembly method ...
}