The if Block.
The while loop.
The for loops.
The switch/case block.
The next/continue statement.
The break statement.
The if block is of the usual form
if(condition1)
statements1
else if(condition2)
statements2
else if(icondition3)
statements3
.
.
.
.
else
statements3
end
Here condition1, condition2, etc. are boolean valued expressions and statements1, statements2 etc. are groups of statements.
The while block is of the usual form
while(condition)
statements
end
Here condition is a boolean valued expression and statements is a group of statements.
A few different forms of the for block are allowed. The
usual Matlab style block is of the form
for varname=first:inc:last
statements
end
or
for varname=first:last
statements
end
in which inc is assumed to be 1.
In these forms of the for block, the group of statements specified by statements is executed as many times as necessary for the
value of the variable varname to range from
first
thru
last
with its value incremented in each execution by the specified
inc.
Depending upon the value of
first
and
inc,
the last execution of the block may or may not correspond the value of
varname
being
last.
The C/Java style of the
for block of the form
for(init;cond;eblock)
statements
end
can also be used. It is equivalent to
init;
while(cond)
statements
eblock;
end
Finally, it is also possible to use the Java
for
block for collections. It is of the form
for(varname:array)
statements
end
where, obviously,
array
is any array. This style is equivalent to
m=numel(array)
for i=1:m
varname=array[i];
statements
end
The switch-case block is of the usual form
switch expression
case value1:
statements1
case value2:
statements2
case value3:
statements3
.
.
.
default:
defaults
end
which is equivalent to
temp=expression
if temp==value1
statements1
else if temp==value2
statements2
else if temp==value3
statements3
.
.
.
else
defaults
end
where
temp is any valid variable name.
The next, or, equivalently, continue statement is used to prematurely jump to the
beginning of a
for
or a
while
loop, typically if some condition is satisfied. Generally, its usage is of the
form
while(condition)
statements1
if(condition)
next
end
statements2
end
or
for varname=first:inc:last
statements1
if(condition)
next
end
statements2
end
Clearly, if the
condition
is satisfied in any of the loops shown above, statements following
the
next
statement are not executed, and the execution jumps to the beginning of the
loop.
Note that
next
can be used in any of the various forms of the
for
loop.
The break statement is used to prematurely terminate a
for
or a
while
loop, typically if some condition is satisfied. Generally, its usage is of the
form
while(condition)
statements1
if(condition)
break
end
statements2
end
or
for varname=first:inc:last
statements1
if(condition)
break
end
statements2
end
Clearly, if the
condition
is satisfied in any of the loops shown above, the loops are prematurely
terminated and the thread of execution jumps to the first statement immediately
following the end of the loop. Note that
break
can be used in any of the various forms of the
for
loop.