while循環(huán)在條件為true時重復執(zhí)行語句。
MATLAB中while循環(huán)的語法是-
while <expression> <statements> end
While 循環(huán)重復執(zhí)行程序語句,只要表達式(statements)保持為 true。
當結果為非空且包含所有非零元素(邏輯或實數值)時,表達式為 true。否則,表達式為 false。
創(chuàng)建一個腳本文件并輸入以下代碼-
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