MATLAB Programming/Control Flow
From Wikibooks, the open-content textbooks collection
Contents |
[edit] Control Flow
[edit] IF statement
An IF statement can be used to execute code once when the logical test (expression) returns a true value (anything but 0). An "else" statement following an if statement is executed if the same expression is false (0).
Syntax:
if expression
statements
elseif expression2
statements
end
[edit] SWITCH statement
Switch statements are used to perform one of several possible sets of operations, depending on the value of a single variable. They are intended to replace nested "if" statements depending on the same variable, which can become very cumbersome. The syntax is as follows:
switch variable
case value1
statements
case value2
statements
...
otherwise
statements
end
The end is only necessary after the entire switch block, not after each case. If you terminate the switch statement and follow it with a "case" statement you will get an error saying the use of the "case" keyword is invalid. If this happens it is probably because you deleted a loop or an "if" statement but forgot to delete the "end" that went with it, thus leaving you with surplus "end"s. Thus MATLAB thinks you ended the switch statement before you intended to.
The otherwise keyword executes a certain block of code (often an error message) for any value of variable other than those specified by the "case" statements.
[edit] TRY/CATCH statement
The TRY/CATCH statement executes a certain block of code in the "try" block. If it fails with an error or a warning, the execution of this code is terminated and the code in the "catch" block is executed rather than simply reporting an error to the screen and terminating the entire program. This is useful for debugging and also for filtering out erroneous calculations, like if you accidentally try to find the inverse of a singular matrix, when you don't wish to end the program entirely.
Syntax:
try
statements
catch
statements
end
Note that unlike the other control flow statements, the TRY/CATCH block does not rely on any conditions. Therefore the code in the TRY block will always be at least partially executed. Not all of the TRY block code will always be executed, since execution of the TRY ends when an error occurs.
[edit] FOR statement
The FOR statement executes code a specified number of times using an iterator. Syntax:
for iterator = startvalue:increment:endvalue
statements
end
The iterator variable is initialized to startvalue and is increased by the amount in increment every time it goes through the loop, until it reaches the value endvalue. If increment is omitted, it is assumed to be 1, as in the following code:
for ii = 1:3
statements
end
This would execute statements three times.
[edit] WHILE statement
The while statement executes code until a certain condition evaluates to false or zero. Example:
while condition
statements
end
Forgetting to change the condition within a while loop is a common cause of infinite loops.
[edit] BREAK and CONTINUE
MATLAB includes the "break" and "continue" keywords to allow tighter loop control. The "break" keyword will cause the program to leave the loop it is currently in and continue from the next line after the loop ends, regardless of the loop's controlling conditions. If the code is in a nested loop it only breaks from the loop it's in, not all of them. The syntax is simply to write the word "break" within the loop where you desire it to break.
In contrast to "break", "continue" causes the program to return back to the beginning of the loop it is presently in, and to recheck the condition to see if it should continue executing loop code or not. The code in the loop after the "continue" statement is not executed in the same pass.