Aqua Phoenix
     >>  Lectures >>  Matlab 4  
 

Navigator
   
 
       
   

4.1 Conditionals

When a program makes a decision between 2 or more possible outcomes, it does so by means of a conditional. In its simplest form, a conditional tests whether or not some expression is valid and performs one or more statements depending on the outcome.

if-then clause

If this lecture is too boring then I will fall asleep.

expression: "this lecture is too boring"

statement: "I will fall asleep"

if-then-else clause

If I fall asleep in lecture then I will not understand the homework assignment otherwise (else) I will be able to do the assignment in 10 minutes.

expression: "I fall asleep in lecture"

statement (if the expression holds): "I will not understand the homework assignment"

statement (if the expression fails): "I will be able to do the assignment in 10 minutes"

For a more mathematical example, consider division of 2 numbers, a and b: a/b. For our purposes, when b=0, the result of a/0 is 0 (even though it is in fact infinity). This operation can easily be implemented in an if-then-else clause as follows:

if (b == 0)
  0
else
  a/b
end
The output of this piece of code is thus a/b if and only if b is not 0.

a=5
b=10
if (b == 0)
  0
else
  a/b
end

Figure 4.1
Click image to enlarge, or click here to open

a=2
b=0
if (b == 0)
  0
else
  a/b
end

Figure 4.2
Click image to enlarge, or click here to open
The expression (b == 0) is internally evaluated as either True or False, i.e. either it holds and b is in fact 0, or it does not hold and b is anything but 0. Common expressions that evaluate to True or False are shown in Table 4.1.

Symbol Example
greater than or equal to >= (a >= b)
greater than > (a > b)
equals == (a == b)
less than < (a < b)
less than or equal to <= (a <= b)
not equal to ~= (7 ~= 6)
Table 4.1
Conditionals with more than 2 cases can be built as well:

if (expression1)
  statement1
elseif (expression2)
  statement2
else
  statement3
end
Expressions within the conditional are not limited to only one test, e.g. (b == 0), but they can be made into more complex decisions. Consider adding two matrices, A and B. In order to add any two matrices, their dimensions must match, that is the number of rows in A must equal the number of rows in B, and the number of columns in A must equal the number of colums in B. Let's assume:

Ar = rows in matrix a

Ac = columns in matrix a

Br = rows in matrix b

Bc = columns in matrix b

The corresponding expression to add the two matrices would be:

if ( (Ar == Br) && (Ac == Bc) )
  add the two matrices
else
  the dimensions don't match
end

In this case the double ampersand requires that both conditions be met, i.e. number of rows in A equal number of rows in B AND number of columns in A equal number of columns in B. Only if both conditions are met is the overall expression satisfied. The double ampersand is called a boolean operator, that is it combines the value of one expression with the value of another expression in boolean terms. While we will not go into details of boolean operators, the most common ones are shown in Table 4.2.

Symbol Example
Both expressions must be valid (True) in order for the overall expression to be valid (True) && (a && b)
Either one of the expressions must be valid (True) in order for the overall expression to be valid (True) || (these symbols can be found on the Backslash key) (a || b)
Table 4.2