Programming and Graphics

(Kiana) #1

F.4Elementary examples 351



  • for loop:


>> for j=-1:1
j
end
j=
-1
j=
0
j=
1

In this example, the first three lines are entered by the user.



  • if statement:


>> j=0;
>> i=1;
>> if i==j+1, disp ’case 1’, end
case 1


  • for loop:


>> n=3;
>> for i=n:-1:2
disp ’i=’; disp (i), end
i=
3
i=
2

The loop is executed backward, starting atn, with step of -1.



  • if loop:


>> i=1; j=2;
>> if i==j+1; disp ’case 1’
elseif i==j; disp ’case2’
else; disp ’case3’
end
case3

In this example, all but the last line are entered by the user.

Free download pdf