Do-While statements implement simple loops.
DoStatement:
do Statement while ( Expression )
Statement is executed. Then Expression is evaluated and must have a type that can be converted to a boolean. If it's true the loop is iterated 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.
For Statement
For statements implement loops with initialization, test, and increment clauses.
ForStatement:
for (Initialize; Test; Increment) Statement
Initialize:
empty
Expression
Declaration
Test:
empty
Expression
Increment:
empty
Expression
Initializer is executed. Test 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 Increment is executed. Then Test is evaluated again, and if true the statement is executed again. This continues until the Test evaluates to false.
A break statement will exit the loop. A continue statement will transfer directly to the Increment.
If Initializer declares a variable, that variable's scope extends through the end of Statement. For example:
for (int i = 0; i < 10; i++)
foo(i);
is equivalent to:
{ int i;
for (i = 0; i < 10; i++)
foo(i);
}
Function bodies cannot be empty:
for (int i = 0; i < 10; i++)
; // illegal
Use instead:
for (int i = 0; i < 10; i++)
{
}
The Initializer may be omitted. Test may also be omitted, and if so, it is treated as if it evaluated to true.
A switch statement goes to one of a collection of case statements depending on the value of the switch expression.
SwitchStatement:
switch ( Expression ) BlockStatement
CaseStatement:
case Expression : Statement
DefaultStatement:
default: Statement
Expression is evaluated. The result type T must be of integral type or char[] or wchar[]. The result is compared against each of the case expressions. If there is a match, the corresponding case statement is transferred to.
If none of the case expressions match, and there is a default statement, the default statement is transferred to.
If none of the case expressions match, and there is not a default statement, a SwitchException is thrown. The reason for this is to catch the common programming error of adding a new value to an enum, but failing to account for the extra value in switch statements.
The case expressions must all evaluate to a constant value or array, and be implicitly convertible to the type T of the switch Expression.
Case expressions must all evaluate to distinct values. There may not be two or more default statements.
Case statements and default statements associated with the switch can be nested within block statements; they do not have to be in the outermost block. For example, this is allowed:
switch (i)
{
case 1:
{
case 2:
}
break;
}
Like in C and C++, case statements 'fall through' to subsequent case values. A break statement will exit the switch BlockStatement. For example:
switch (i)
{
case 1:
x = 3;
case 2:
x = 4;
break;
case 3:
x = 5;
break;
}
will set x to 4 if i is 1.
Note: Unlike C and C++, strings can be used in switch expressions. For example:
char[] name;
...
switch (name)
{
case "fred":
case "sally":
...
}
For applications like command line switch processing, this can lead to much more straightforward code, being clearer and less error prone. Both ascii and wchar strings are allowed.
Implementation Note: The compiler's code generator may assume that the case statements are sorted by frequency of use, with the most frequent appearing first and the least frequent last. Although this is irrelevant as far as program correctness is concerned, it is of performance interest.
A continue aborts the current iteration of its enclosing loop statement, and starts the next iteration.
ContinueStatement:
continue;
continue Identifier ;
continue executes the next iteration of its innermost enclosing while, for, or do loop. The increment clause is executed.
If continue is followed by Identifier, the Identifier must be the label of an enclosing while, for, or do loop, and the next iteration of that loop is executed. It is an error if there is no such statement.
Any intervening finally clauses are executed, and any intervening synchronization objects are released.
Note: If a finally clause executes a return, throw, or goto out of the finally clause, the continue target is never reached.
Share with your friends: |