Department of Engineering

IT Services

Constructs

MATLAB provides the for, while and if constructs. MATLAB will wait for you to type the end statement before it executes the construct.


>> y=[];

>> for x=0.5:0.5:5
y=[y,2*x];
end
>> y
>> n=3;
>> while(n ~= 0)
n=n-1
end
>> if(n < 0)
-n
elseif (n == 0)
n=365
else
n
end

The three numbers in the for statement specify start, step and end values for the loop. Note that you can print a variable's value out by mentioning its name alone on the line. ~= means `not equal to' and == means `equivalent to'.