
4.2.1 The
i
i
f
f Statement
The
i
i
f
f statement evaluates a logical expression and executes a group of
statements when the expression is TRUE. The optional
e
e
l
l
s
s
e
e
i
i
f
f and
e
e
l
l
s
s
e
e
keywords can be used for the execution of alternate groups of statements.
An
e
e
n
n
d
d
keyword, which matches the
i
i
f
f, terminates the last group of
statements. The groups of statements are delineated by the four above
keywords-no braces or brackets are involved [2]. The three possible syntax
versions of this conditional expression are tabulated in Table 7.
Syntax Example
if logical expression
segment of programming code executed
if the logical expression is TRUE
end
flag=1;
if ~isempty(flag)
disp('Hello')
end
if logical expression
segment of programming code executed
if the logical expression is TRUE
else
segment of programming code executed
if the logical expression is FALSE
end
sales=5000;
if sales<1000
Profit=sales*0.1;
else
Profit=(sales-1000)*0.2+1000*0.1;
end
if logical expression #1
segment of programming code executed
if the logical expression #1 is TRUE
elseif logical expression #2
segment of programming code executed
if the logical expression #2 is TRUE
else
segment of programming code executed
if the previous logical expressions are
FALSE
end
sales=5000;
if (sales>1000 & sales<=2000)
disp('Low Sales');
Profit=sale*0.1;
elseif (sales>2000 & sales<=10000)
disp('Medium Sales ');
Profit=sales*0.15;
else
disp('Satisfactory Sales ');
Profit=sales*0.17;
end
Table 7: The
i
i
f
f statement syntax and examples.
Komentáře k této Příručce