while語法:
while expression
statements
end
說明:while expression, statements, end 計算壹個表達式,並在該表達式為 true 時在壹個循環中重復執行壹組語句。表達式的結果非空並且僅包含非零元素(邏輯值或實數值)時,該表達式為 true。否則,表達式為 false。
示例代碼如下:
function [sum] = summation(ratio, head, top)
sum = 0;
while (head <= top)
sum = sum + ratio ^ head;
head = head + 1;
end
end
假設ratio = 2,head = 0,top = 63
擴展資料:
Matlab控制流語句包括條件語句、循環和分支。
if, elseif, else條件為 true 時執行語句。
for用來重復指定次數的 for 循環。
parfor並行循環。
switch, case, otherwise執行多組語句中的壹組。
try, catch執行語句並捕獲產生的錯誤。
while條件為 true 時重復執行的 while 循環。
break終止執行 for 或 while 循環。
continue將控制權傳遞給 for 或 while 循環的下壹叠代。
end終止代碼塊或指示最大數組索引。
pause暫時停止執行 MATLAB。
return將控制權返回給調用函數。
參考資料:
MathWorks文檔——while循環