个性化阅读
专注于IT技术分析

MATLAB while循环语句

本文概述

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

句法

while <expression>
 <statements>
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

赞(0)
未经允许不得转载:srcmini » MATLAB while循环语句

评论 抢沙发

评论前必须登录!