D programming Language



Download 1.66 Mb.
Page12/47
Date08.01.2017
Size1.66 Mb.
#7507
1   ...   8   9   10   11   12   13   14   15   ...   47

Statements


C and C++ programmers will find the D statements very familiar, with a few interesting additions.

Statement:

LabeledStatement

BlockStatement

ExpressionStatement

DeclarationStatement

IfStatement

DebugStatement

VersionStatement

WhileStatement

DoWhileStatement

ForStatement

SwitchStatement

CaseStatement

DefaultStatement

ContinueStatement

BreakStatement

ReturnStatement

GotoStatement

WithStatement

SynchronizeStatement

TryStatement

ThrowStatement

VolatileStatement

AsmStatement


  • Labeled Statements

  • Block Statement

  • Expression Statement

  • Declaration Statement

  • If Statement

  • Debug Statement

  • Version Statement

  • While Statement

  • Do-While Statement

  • For 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


Labelled Statements


Statements can be labelled. A label is an identifier that precedes a statement.

LabelledStatement:

Identifier ':' Statement

Any statement can be labelled, including empty statements, and so can serve as the target of a goto statement. Labelled statements can also serve as the target of a break or continue statement.

Labels are in a name space independent of declarations, variables, types, etc. Even so, labels cannot have the same name as local declarations. The label name space is the body of the function they appear in. Label name spaces do not nest, i.e. a label inside a block statement is accessible from outside that block.


Block Statement


A block statement is a sequence of statements enclosed by { }. The statements are executed in lexical order.

BlockStatement:

{ }

{ StatementList }
StatementList:

Statement

Statement StatementList

A block statement introduces a new scope for local symbols. A local symbol's name, however, must be unique within the function.

void func1(int x)

{ int x; // illegal, x is multiply defined in function scope

}
void func2()

{

int x;


{ int x; // illegal, x is multiply defined in function scope

}

}


void func3()

{

{ int x;



}

{ int x; // illegal, x is multiply defined in function scope

}

}
void func4()



{

{ int x;


}

{ x++; // illegal, x is undefined

}

}

The idea is to avoid bugs in complex functions caused by scoped declarations inadvertantly hiding previous ones. Local names should all be unique within a function.




Expression Statement


The expression is evaluated.

ExpressionStatement:

Expression ;

Expressions that have no affect, like (x + x), are illegal in expression statements.




Declaration Statement


Declaration statements declare and initialize variables.

DeclarationStatement:

Type IdentifierList ;
IdentifierList:

Variable

Variable , IdentifierList
Variable:

Identifier

Identifier = AssignmentExpression

If no AssignmentExpression is there to initialize the variable, it is initialized to the default value for its type.




If Statement


If statements provide simple conditional execution of statements.

IfStatement:

if ( Expression ) Statement

if ( Expression ) Statement else Statement

Expression is evaluated and must have a type that can be converted to a boolean. If it's true the if statement is transferred to, else the else statement is transferred to.

The 'dangling else' parsing problem is solved by associating the else with the nearest if statement.




While Statement


While statements implement simple loops.

WhileStatement:

while ( Expression ) Statement

Expression is evaluated and must have a type that can be converted to a boolean. If it's true the statement is executed. After the statement is executed, the Expression is evaluated again, and if true the statement is executed again. This continues until the Expression evaluates to false.

A break statement will exit the loop. A continue statement will transfer directly to evaluationg Expression again.






Download 1.66 Mb.

Share with your friends:
1   ...   8   9   10   11   12   13   14   15   ...   47




The database is protected by copyright ©ininet.org 2024
send message

    Main page