while loop is a conditional loop
即系,满足条件时,会一直循环重复执行,,,直到 不满足 条件时才能结束循环
在MATLAB 中 while循环的语法是:
while <expression> <statements> end
while 循环反复执行程序语句只要表达式为 true。
表达式是 true,当结果不为空,并包含所有非零元素(逻辑或实际数字)。否则,表达式为 false。
a =10;
% while loop execution
while( a < 20 )
fprintf('value of a: %d', 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
>>
alike = input ('how much do you like the dreamy?');
while ((alike < 1) || ( alike > 10))
fprintf('Error! Invalid input! Please enter number from 1 to 10.\r\n');
alike = input ('how much do you like the dreamy?');
end
alike = input ('how much do you like the dreamy?');
while ((alike < 1) || ( alike > 10))
fprintf('Error! Invalid input! Please enter number from 1 to 10.\r\n');
alike = input ('how much do you like the dreamy?');
end
how much do you like the dreamy?
11
Error! Invalid input! Please enter number from 1 to 10.
how much do you like the dreamy?
1