Getting Started with MATLAB   Search    Help Desk 

Flow Control

MATLAB has five flow control constructs:

if

The if statement evaluates a logical expression and executes a group of statements when the expression is true. The optional elseif and else keywords provide for the execution of alternate groups of statements. An end keyword, which matches the if, terminates the last group of statements. The groups of statements are delineated by the four keywords - no braces or brackets are involved.

MATLAB's algorithm for generating a magic square of order n involves three different cases: when n is odd, when n is even but not divisible by 4, or when n is divisible by 4. This is described by

In this example, the three cases are mutually exclusive, but if they weren't, the first true condition would be executed.

It is important to understand how relational operators and if statements work with matrices. When you want to check for equality between two variables, you might use

This is legal MATLAB code, and does what you expect when A and B are scalars. But when A and B are matrices, A == B does not test if they are equal, it tests where they are equal; the result is another matrix of 0's and 1's showing element-by-element equality. In fact, if A and B are not the same size, then
A == B is an error.

The proper way to check for equality between two variables is to use the
isequal function,

Here is another example to emphasize this point. If A and B are scalars, the following program will never reach the unexpected situation. But for most pairs of matrices, including our magic squares with interchanged columns, none of the matrix conditions A > B, A < B or A == B is true for all elements and so the else clause is executed.

Several functions are helpful for reducing the results of matrix comparisons to scalar conditions for use with if, including

switch and case

The switch statement executes groups of statements based on the value of a variable or expression. The keywords case and otherwise delineate the groups. Only the first matching case is executed. There must always be an end to match the switch.

The logic of the magic squares algorithm can also be described by

NOTE FOR C PROGRAMMERS
Unlike the C language switch statement,
MATLAB's switch does not fall through. If the first case statement is true, the other case statements do not execute. So, break statements are not required.

for

The for loop repeats a group of statements a fixed, predetermined number of times. A matching end delineates the statements.

The semicolon terminating the inner statement suppresses repeated printing, and the r after the loop displays the final result.

It is a good idea to indent the loops for readability, especially when they are nested.

while

The while loop repeats a group of statements an indefinite number of times under control of a logical condition. A matching end delineates the statements.

Here is a complete program, illustrating while, if, else, and end, that uses interval bisection to find a zero of a polynomial.

The result is a root of the polynomial x3 - 2x - 5, namely

The cautions involving matrix comparisons that are discussed in the section on the if statement also apply to the while statement.

break

The break statement lets you exit early from a for or while loop. In nested loops, break exits from the innermost loop only.

Here is an improvement on the example from the previous section. Why is this use of break a good idea?



[ Previous | Help Desk | Next ]