📜  MATLAB while循环

📅  最后修改于: 2021-01-07 02:16:19             🧑  作者: Mango

MATLAB while循环

while循环重复执行语句,而指定的语句为true。

句法:

 while 
 
end

范例1:

% program to find the number ten from a series of random numbers
% using while loop
k = 1;
while k
    if randi(50,1) == 10
        disp(['The random number equivalent to 10 found at ',num2str(k),' step'])
        break
    end
    k = k + 1;
end

输出:

The random number equivalent to 10 found at 36 step

范例2:

a = 10;
% while loop execution
while( a < 20 )
 fprintf('value of a: %d\n', a);
 a = a + 1;
end

输出:

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19