Iteration statements repeatedly execute an embedded statement.
iteration-statement:
while-statement
do-statement
for-statement
foreach-statement
15.8.1 The while statement
The while statement conditionally executes an embedded statement zero or
more times.
Chapter 15 Statements
187
while-statement:
while ( boolean-expression ) embedded-statement
A while statement is executed as follows:
? The boolean-expression (§14.16) is evaluated.
? If the boolean expression yields true, control is transferred to the
embedded statement. When and if
control reaches the end point of the embedded statement (possibly from
execution of a continue
statement), control is transferred to the beginning of the while statement.
? If the boolean expression yields false, control is transferred to the end
point of the while statement.
Within the embedded statement of a while statement, a break statement (§15.9
.1) may be used to transfer
control to the end point of the while statement (thus ending iteration of
the embedded statement), and a
continue statement (§15.9.2) may be used to transfer control to the end
point of the embedded statement
(thus performing another iteration of the while statement).
The embedded statement of a while statement is reachable if the while
statement is reachable and the
boolean expression does not have the constant value false.
The end point of a while statement is reachable if at least one of the
following is true:
? The while statement contains a reachable break statement that exits the
while statement.
? The while statement is reachable and the boolean expression does not have
the constant value true.
15.8.2 The do statement
The do statement conditionally executes an embedded statement one or more
times.
do-statement:
do embedded-statement while ( boolean-expression ) ;
A do statement is executed as follows:
? Control is transferred to the embedded statement.
? When and if control reaches the end point of the embedded statement
(possibly from execution of a
continue statement), the boolean-expression (§14.16) is evaluated. If the
boolean expression yields
true, control is transferred to the beginning of the do statement.
Otherwise, control is transferred to the
end point of the do statement.
Within the embedded statement of a do statement, a break statement (§15.9.1)
may be used to transfer
control to the end point of the do statement (thus ending iteration of the
embedded statement), and a
continue statement (§15.9.2) may be used to transfer control to the end
point of the embedded statement
(thus performing another iteration of the do statement).
The embedded statement of a do statement is reachable if the do statement
is reachable.
The end point of a do statement is reachable if at least one of the
following is true:
? The do statement contains a reachable break statement that exits the do
statement.
? The end point of the embedded statement is reachable and the boolean
expression does not have the
constant value true.
15.8.3 The for statement
The for statement evaluates a sequence of initialization expressions and
then, while a condition is true,
repeatedly executes an embedded statement and evaluates a sequence of
iteration expressions.
for-statement:
for ( for-initializeropt ; for-conditionopt ; for-iteratoropt )
embedded-statement
C# LANGUAGE SPECIFICATION
188
for-initializer:
local-variable-declaration
statement-expression-list
for-condition:
boolean-expression
for-iterator:
statement-expression-list
statement-expression-list:
statement-expression
statement-expression-list , statement-expression
The for-initializer, if present, consists of either a
local-variable-declaration (§15.5.1) or a list of statementexpressions
(§15.6) separated by commas. The scope of a local variable declared by a
for-initializer starts at
the local-variable-declarator for the variable and extends to the end of
the embedded statement. The scope
includes the for-condition and the for-iterator.
The for-condition, if present, must be a boolean-expression (§14.16).
The for-iterator, if present, consists of a list of statement-expressions (§
15.6) separated by commas.
A for statement is executed as follows:
? If a for-initializer is present, the variable initializers or statement
expressions are executed in the order
they are written. This step is only performed once.
? If a for-condition is present, it is evaluated.
? If the for-condition is not present or if the evaluation yields true,
control is transferred to the embedded
statement. When and if control reaches the end point of the embedded
statement (possibly from
execution of a continue statement), the expressions of the for-iterator, if
any, are evaluated in
sequence, and then another iteration is performed, starting with evaluation
of the for-condition in the
step above.
? If the for-condition is present and the evaluation yields false, control
is transferred to the end point of
the for statement.
Within the embedded statement of a for statement, a break statement (§15.9.1
) may be used to transfer
control to the end point of the for statement (thus ending iteration of the
embedded statement), and a
continue statement (§15.9.2) may be used to transfer control to the end
point of the embedded statement
(thus executing another iteration of the for statement).
The embedded statement of a for statement is reachable if one of the
following is true:
? The for statement is reachable and no for-condition is present.
? The for statement is reachable and a for-condition is present and does
not have the constant value
false.
The end point of a for statement is reachable if at least one of the
following is true:
? The for statement contains a reachable break statement that exits the for
statement.
? The for statement is reachable and a for-condition is present and does
not have the constant value
true.
15.8.4 The foreach statement
The foreach statement enumerates the elements of a collection, executing an
embedded statement for each
element of the collection.
foreach-statement:
foreach ( type identifier in expression ) embedded-statement
Chapter 15 Statements
189
The type and identifier of a foreach statement declare the iteration
variable of the statement. The iteration
variable corresponds to a read-only local variable with a scope that
extends over the embedded statement.
During execution of a foreach statement, the iteration variable represents
the collection element for which
an iteration is currently being performed. A compile-time error occurs if
the embedded statement attempts to
modify the iteration variable (via assignment or the ++ and -- operators)
or pass the iteration variable as a
ref or out parameter.
The type of the expression of a foreach statement must be a collection type
(as defined below), and an
explicit conversion (§13.2) must exist from the element type of the
collection to the type of the iteration
variable. If expression has the value null, a System.NullReferenceException
is thrown.
A type C is said to be a collection type if it implements the
System.Collections.IEnumerable
interface or implements the collection pattern by meeting all of the
following criteria:
? C contains a public instance method with the signature GetEnumerator(),
that returns a struct-type,
class-type, or interface-type, which is called E in the following text.
? E contains a public instance method with the signature MoveNext() and the
return type bool.
? E contains a public instance property named Current that permits reading
the current value. The type
of this property is said to be the element type of the collection type.
A type that implements IEnumerable is also a collection type, even if it
doesn’t satisfy the conditions
above. (This is possible if it implements IEnumerable via explicit
interface member implementations.)
The System.Array type (§19.1.1) is a collection type, and since all array
types derive from
System.Array, any array type expression is permitted in a foreach
statement. The order in which
foreach traverses the elements of an array is as follows: For
single-dimensional arrays elements are
traversed in increasing index order, starting with index 0 and ending with
index Length ? 1. For multidimensional
arrays, elements are traversed such that the indices of the rightmost
dimension are increased
first, then the next left dimension, and so on to the left.
A foreach statement of the form:
foreach (ElementType element in collection) statement
corresponds to one of two possible expansions:
? If the collection expression is of a type that implements the collection
pattern (as defined above), the
expansion of the foreach statement is:
Enumerator enumerator = (collection).GetEnumerator();
try {
while (enumerator.MoveNext()) {
ElementType element = (ElementType)enumerator.Current;
statement;
}
}
finally {
IDisposable disposable = enumerator as System.IDisposable;
if (disposable != null) disposable.Dispose();
}
[Note: Significant optimizations of the above are often easily available.
If the type E implements
System.IDisposable, then the expression (enumerator as System.IDisposable)
will always
be non-null and the implementation can safely substitute a simple
conversion for a possibly more
expensive type test. Conversely, if the type E is sealed and does not
implement
System.IDisposable, then the expression (enumerator as System.IDisposable)
will
always evaluate to null. In this case, the implementation can safely
optimize away the entire finally
clause. end note]
? Otherwise; the collection expression is of a type that implements
System.Collections.IEnumerable, and the expansion of the foreach statement
is:
C# LANGUAGE SPECIFICATION
190
IEnumerator enumerator =
((System.IEnumerable)(collection)).GetEnumerator();
try {
while (enumerator.MoveNext()) {
ElementType element = (ElementType)enumerator.Current;
statement;
}
}
finally {
IDisposable disposable = enumerator as System.IDisposable;
if (disposable != null) disposable.Dispose();
}
In either expansion, the enumerator variable is a temporary variable that
is inaccessible in, and invisible
to, the embedded statement, and the element variable is read-only in the
embedded statement.
[Example: The following example prints out each value in a two-dimensional
array, in element order:
using System;
class Test
{
static void Main() {
double[,] values = {
{1.2, 2.3, 3.4, 4.5},
{5.6, 6.7, 7.8, 8.9}
};
foreach (double elementValue in values)
Console.Write("{0} ", elementValue);
Console.WriteLine();
}
}
The output produced is as follows:
1.2 2.3 3.4 4.5 5.6 6.7 7.8 8.9
end example]
分享到:
相关推荐
Python库`iteration_utilities`是一个强大的工具包,专为处理序列数据和迭代器设计。这个库提供了许多高效且灵活的函数,使得在处理复杂迭代逻辑时能够更加简洁和高效。`iteration_utilities-0.11.0-cp36-cp36m-win...
策略迭代(Policy Iteration)是强化学习中的一种经典算法,适用于离散状态和动作空间的问题。本篇文章将深入探讨如何在Python中实现策略迭代算法。 策略迭代算法主要包括两个步骤:策略评估(Policy Evaluation)...
本文将深入探讨"Improved_iteration"算法,它是数值分析中用于求解积分的一种方法。这个算法在C语言和C++编程环境中得到了应用。 "Improved_iteration"算法是一种改进的迭代方法,主要用于近似求解积分。在数学中,...
标题中的"Newton-iteration-method.rar_Newton-iteration_newton_newton itera"暗示了压缩包包含的可能是一个关于牛顿迭代法的程序或脚本,用于解决数值计算中的方程求解问题。这个程序可能设计为用户输入初始猜测值...
标题中的"Newton-Iteration.zip_newton_newton iteration"暗示了这是一个关于牛顿迭代法的资料包,可能包含程序代码、示例或教程,用于说明如何应用该方法。"newton"和"newton_iteration"这两个标签进一步强调了主题...
* 迭代语句(Iteration statements):用于重复执行某些操作,例如for循环、while循环等。 * Try块语句:用于捕捉和处理异常。 程序结构 C++程序的结构可以分为多个部分,包括: * main函数:程序的入口函数,...
本资源“Dynamic-Programming-master.zip”包含了基于策略迭代(Policy Iteration)和值迭代(Value Iteration)的动态规划算法实现,主要用于解决机器人的最优运输问题。 **策略迭代(Policy Iteration)**: 策略...
Iteration of rational functions by Alan F. Beardon (z-lib.org).pdf
### 迭代方法在侧向抛物方程中的应用 #### 概述 本文讨论了在四分之一平面上的一个反向热传导问题(IHCP),其中数据在 \(x=1\) 处给出。该问题被称为侧向抛物方程,并且是严重不适定的。虽然已有的数值方法如...
复解析动力系统方面入门的比较基础的书,写的简单细致,很不错
The Fundamentals of Local Fractional Iteration of the Continuously Nondifferentiable Functions Derived form Local Fractional Calculus,杨小军,,A new posssible modeling for local fractional iteration...
标题中的"aduna-commons-iteration-2.6.0.jar.zip"是一个压缩文件,它包含了一个名为"aduna-commons-iteration-2.6.0.jar"的Java Archive(JAR)文件。Aduna Commons Iteration是Aduna组织开发的一个Java库,专门...
标题中的"aduna-commons-iteration-2.2.jar.zip"是一个Java库的压缩文件,主要包含名为"aduna-commons-iteration-2.2.jar"的Java档案(JAR)文件。这个JAR文件是Java平台上的一个可执行文件格式,通常用于封装多个...
Control program flow with decision and iteration statements Build more robust apps with error, exception, and resource management Master the essentials of Visual C# object-oriented programming Use ...
标题中的"aduna-commons-iteration-2.7.0.jar.zip"是一个Java库的压缩文件,其中包含的"aduna-commons-iteration-2.7.0.jar"是核心的Java档案(JAR)文件。这个JAR文件是Aduna Commons Iteration库的版本2.7.0,它是...
这是 一个数学基本运算中,关于线性方程迭代算法的一个程序,程序设计规范,可以直接调用
在这个主题中,我们将深入探讨基于Python的值迭代算法(Value Iteration)实现,这是一种解决有限马尔科夫决策过程(Finite Markov Decision Process, MDP)的经典方法。 值迭代算法是强化学习的基础算法之一,它...
标题中的"macro_valueiteration_VFImatlab_zip_"表明这是一个关于马尔科夫决策过程(Markov Decision Process, MDP)的宏(macro)实现,使用MATLAB编程语言,具体是针对值函数迭代(Value Iteration)算法的。...