Octave快速入门(5)——控制语句

2018-09-12  本文已影响0人  大锅烩菜

for循环

>>v = zeros(10,1);

>>for i=1:10,
>     v(i)=2^i;
>   end;

>>v
v =
      2
      4
      8
     16
     32
     64
    128
    256
    512
   1024
>>

或者

>> indices = 1:10;
>> for i = indices,
       disp(i);
end;
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
>>

while

>>i = 1;
>>while i<=5,
>    v(i)=100;
>     i = i+1;
> end;

break

>>i = 1;
>>while true,
>     v(i) = 999;
>     i = i + 1;
>     if i==6,
>        break;
>     end;
> end;

if...else

>>if v(1) == 1,
>     disp('The value is one');
> elseif v(1) == 2,
>     disp('The value is Two');
> else
>     disp('The value is not one or two');
> end;
上一篇 下一篇

猜你喜欢

热点阅读