MATLAB while循环语句

本文概述

  • 句法
  • 例1
  • 例2
while循环重复执行语句, 而指定的语句为true。
句法
while < expression> < statements> end

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

输出
The random number equivalent to 10 found at 36 step

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

【MATLAB while循环语句】输出
value of a: 10value of a: 11value of a: 12value of a: 13value of a: 14value of a: 15value of a: 16value of a: 17value of a: 18value of a: 19

    推荐阅读