while
Repeat statements an indefinite number of times
循环语句,一个次数的不定数
Syntax
语法
while expression
statements
End
while 表达式
语句
End
Description
描述
while repeats statements an indefinite number of times. The statements are executed while the real part of expression has all nonzero elements. expression is usually of the form
while 循环语句,一个次数的不定数。语句是执行循环表达式的实部有所有非零元素。表达式是通常形式。
expression rel_op expression
表达式 rel_op 表达式
where rel_op is ==, <, >, <=, >=, or ~=.
rel_op 部分是 ==,<,>,<=,>=,或~=。
The scope of a while statement is always terminated with a matching end.
一个循环语句的范围通常是以一个匹配的end结束。
Argumentsa
论点
Expression
表达式
expression is a MATLAB expression, usually consisting of variables or smaller expressions joined by relational operators (e.g., count < limit) or logical functions (e.g., isreal(A)).
表达式是一个MATLAB表达式,通常包括变量或较小的语句通过比例式算子参与。(e.g.,count<limit)或逻辑函数(e.g.,sireal(A))。
Simple expressions can be combined by logical operators (&, |, ~) into compound expressions such as the following. MATLAB evaluates compound expressions from left to right, adhering to operator precedence rules.
简单的表达式能够通过逻辑算子联系(&,|,~)进入复合表达式例如如下。MATLAB评价复合表达式从左到右,附着算子领先规定。
(count < limit) & ((height - offset) >= 0)
Statements
语句
statements is one or more MATLAB statements to be executed only while the expression is true or nonzero.
语句是一个或多个MATLAB语句来执行仅仅循环表达式是正确或非零。
Remarks
备注
Nonscalar Expressions
非标量表达式
If the evaluated expression yields a nonscalar value, then every element of this value must be true or nonzero for the entire expression to be considered true. For example, the statement while (A < B) is true only if each element of matrix A is less than its corresponding element in matrix B. See Example 2, below.
如果评定表达式收益一个非标量值,则任意这个值得元素必须是正确或非零为整个表达式来考虑正确。例如,语句while(A<B)是正确仅如果矩阵A的每个元素是少与它的在矩阵B中对应的元素。查看例2,下面。
Partial Evaluation of the Expression Argument
表达式论点的片面评价
Within the context of an if or while expression, MATLAB does not necessarily evaluate all parts of a logical expression. In some cases it is possible, and often advantageous, to determine whether an expression is true or false through only partial evaluation.
if或while 表达式的内部语境,MATLAB不是必然评价所有逻辑表达的部分。在一些事件中它是可能的,并且常常有利,通过部分鉴定来确定一个表示式是true或false。
For example, if A equals zero in statement 1 below, then the expression evaluates to false, regardless of the value of B. In this case, there is no need to evaluate B and MATLAB does not do so. In statement 2, if A is nonzero, then the expression is true, regardless of B. Again, MATLAB does not evaluate the latter part of the expression.
例如,如果在下面的语句1中A对等零,则无论如何B的值是什么,表达式鉴定为false。在这个事件中,那里是不需要鉴定B并且MATLAB不会做任何操作。在语句2种,如果A是非零,则无论B为什么,表达式为true。再次表明,MATLAB不在下面的表达式中做任何鉴定。
1) while (A & B) 2) while (A | B)
You can use this property to your advantage to cause MATLAB to evaluate a part of an expression only if a preceding part evaluates to the desired state. Here are some examples.
你能够使用特性用来有助于你利用MATLAB来鉴定一个表达式的一个部分,如果一个前部分鉴定状态。这里一些例子。
while (b ~= 0) & (a/b > 18.5)
if exist('myfun.m') & (myfun(x) >= y)
if iscell(A) & all(cellfun('isreal', A))
Examples
例如
Example 1 - Simple while Statement
例1 简单while 语句
The variable eps is a tolerance used to determine such things as near singularity and rank. Its initial value is the machine epsilon, the distance from 1.0 to the next largest floating-point number on your machine. Its calculation demonstrates while loops.
eps变量是一个公差用来确定这样的事态作为接近特性和阶。它的初始值是机械,距离从1.0到下一个最大的浮点数值在你的机器上。它的计算表明循环回路。
eps = 1;
while (1+eps) > 1
eps = eps/2;
end
eps = eps*2
Example 2 - Nonscalar Expression
例2 非标量表达式
Given matrices A and B,
给出矩阵A和B,
A = B =
1 0 1 1
2 3 3 4
Expression 表达式 |
Evaluates As 鉴定为 |
Because 原因 |
A < B |
false |
A(1,1) is not less than B(1,1). A(1,1)不小于B(1,1)。 |
A < (B + 1) |
true |
Every element of A is less than that same element of B with 1 added. A的任意元素小于那个B+1的相同元素 |
A & B |
false |
A(1,2) & B(1,2) is false. A(1,2)&B(1,2)是false。 |
B < 5 |
true |
Every element of B is less than 5. B的任意元素小于5。 |