Control : for, if, ...



Flow control is accomplished using if, for, while, and break statements with the following syntax..

  if(conditional) Matlab statements end
  if(conditional) Matlab statements elseif (conditional) Matlab statements else Matlab statements end
  for( i = iBegin : iEnd) Matlab statements end
  for( i = iBegin : increment : iEnd) Matlab statements end
  while(conditional) Matlab statements end
  break

Logical operands used in conditional expressions are

	==	equality
	~=	not equal
	|	or 
	&	and

0 indicates the boolean value FALSE while any non-zero value indicates the boolean value TRUE.

Samples

% A sample if statement. 

if((i < 10)&(i >0))
  x(i) = 10.0;
else
  x(i) = 20.0;
  i    = i+1;
end

% A for loop from i = 1 to i = 3 in increments of 1

for(i = 1:3)
    x(i) = i;
    y(i) = i-1;
end

% A for loop from i = 5 to i = 1 in increments of -1

for(i =5:-1:1)
    x(i) = i;
end

UCLA Mathematics Department ©2000