An IF statement will execute equations subject to a logical condition
Statements within an IF Statement are executed subject to a logical condition being satisfied. For example the following logical expression would be TRUE if x was greater than 1, and FALSE if x was less than (or equal to) 1.
x > 1The list of relational operators and their meanings are:
Relational Operator Meaning
< less than
<= less than or equal to
== equal to
~= not equal to
> greater than
>= greater than or equal to
Expressions can be combined using the logical operators AND, OR, and NOT. The MATLAB symbols for these are:
Logical Operator Meaning
& AND
: OR
~ NOT
Consider the following example; if we assume that x>y is true (i.e. x is greater than y) and that x < 10 is false, the following expressions return the values indicated:
Expression x>y & x<10 FALSE x>y : x<10 TRUE ~ x>y FALSE x>y & (~ x<10) TRUE
The IF statement is used to determine, when the program is running, whether a block of statements should be executed. It can also be used to execute alternative sequences of statements, depending on alternative conditions. An example of the simplest form of IF statement is shown below:
...
if x > y
'X is greater than Y and difference is '
diff = x-y
end
...
The IF statement takes as its argument a logical expression,
as described above. If that expression evaluates to TRUE, the
statements that immediately follow the IF statement are
executed. If the condition is not true (i.e. it is FALSE), execution
proceeds with the statements after the END statement. In the
example, if x were greater than y, a difference would be calculated
and a message printed out. If x were less than or equal to y,
execution would proceed immediately to the rest of the code indicated
by the second set of ...'s.
A more general form of IF statement includes a second block of statements which are executed if the logical expression is false.
...
if x > y
'X is greater than Y. The difference is '
diff = x - y
else
'In this case, X is less than Y. The difference is'
diff = y - x
end
...
This case differs from the previous example in that if x is not greater than
y, a different method is used to calculate the difference and a slightly
different message is printed out to the user. In general, if the argument to
the IF statement is FALSE, execution proceeds to the statement
immediately following the ELSE statement and continues until the
END statement is reached.
A slight variation on this form of IF statement is one in which a series of conditions is tested. An example of this is shown below where the user is asked for the pressure (in atmospheres) of a vessel. This value is then checked against some criteria and if unsuitable, the user is asked to re-enter the data.
...
P = input('Please input the pressure of the vessel ');
if P < 1.0
'Sub-atmospheric pressure not allowed.'
P = input('Please re-enter the pressure of the vessel ');
elseif P > 10.0
'Pressure is too high!'
P = input('Please re-enter the pressure of the vessel ');
else
'Pressure appears to be within valid range.';
end
...
The tests of the logical expressions are made one after the other until one
is true. The corresponding block of statements is then executed and the
control is passed to the END statement ignoring any other tests
that have not been done yet. If none of the conditions is true, the final
ELSE clause (if such is present) is executed.
Finally we can have nested IF statements but each has to be fully contained in a block of the next outer IF statement. For example, to compare three quantities:
if x > y
if x > z
'X would appear to be the largest value.'
else
'Z is the largest value.'
end
end
This example is not complete. What code do you need to write to cover all
the cases? How can you rewrite this code using logical expressions to make
it shorter (and hopefully easier to read!)?
Click HERE for next section.