Program control statements determine the flow of program execution, including conditional control, loop control, error control, and termination control.
Conditional Control
Conditional control allows the program to selectively execute blocks of code. When the condition can be answered with "yes" or "no," use the if statement; when the condition can have multiple options based on the value of an expression, use the switch and case statements.
1. if/elseif Statements
The simplest if/elseif statement is as follows:
if expression
statements;
end
Here, expression is a logical expression, and statements are one or more lines of statements. If the value in the expression is true, the code between if and end is executed; otherwise, it is skipped. If there are two options, use the following if/else statement:
if expression
statements;
else
statements;
end
expression is a logical expression, and statements are one or more lines of statements. When there are multiple choices, you can use a similar if/elseif statement.
2. switch/case Statements
The general format of this statement is:
switch switch_expression
case case_expression1
statements1;
case case_expression2
statements2;
case case_expression3
statements3;
...
otherwise
statements;
end
Here, switch_expression gives the switching condition. When a case_expressionN matches it, the statements statementsN following it are executed. If no case_expressionN matches, the statements following otherwise are executed. During execution, only one case command is executed. After the command is executed, the program jumps out of the branch structure and executes the statement below end.
Table 3-4 lists the differences between switch/case statements and if/elseif statements.
Table 3-4: Differences Between switch/case Statements and if/elseif Statements
| switch/case Statement | if/elseif Statement |
|---|---|
| More readable | Harder to read |
| Can compare strings of different lengths | Requires strcmp function to compare strings of different lengths |
| Only tests for equality | Tests for equality or inequality |
Note: Both C language and MATLAB language have switch/case statements, but they differ: in MATLAB's case statement, you can specify string values, whereas in C language you cannot.
[Example 3-4]
The following code displays different content based on the value of method. When the value is linear, it displays "Method is linear method." When the value is cubic, it displays "Method is cubic method."
switch(method)
case 'linear'
disp('Method is linear method.')
case 'cubic'
disp('Method is cubic method.')
end
You can use a switch statement to test for more than one case. The following example uses a cell array in the case statement to test for a linear method or a bilinear method.
switch(method)
case {'linear', 'bilinear'}
disp('Method is linear or bilinear method.')
...
end
In C language, if a break statement is not used to terminate each case block, the code execution will fall through to the next case block. However, this does not happen in MATLAB language; only one case block will run. Using a break statement within a case block is not only unnecessary but also illegal and will generate a warning.
[Example 3-5] If result equals 52, only the first disp statement runs. Although the second case also meets the requirement, it will not run.
switch(result)
case 52
disp('Result is 52')
case {52, 78}
disp('Result is 52 or 78')
end
Loop Control
Using loop control statements allows you to execute blocks of code repeatedly. The for statement is used when the number of executions is specified; the while statement is suitable for situations where the loop executes until a condition is met; and the continue and break statements provide more control over exiting loops.
Using loop control statements allows you to execute blocks of code repeatedly. The for statement is used when the number of executions is specified; the while statement is suitable for situations where the loop executes until a condition is met; and the continue and break statements provide more control over exiting loops.
1. for Loop
A for loop allows a set of commands to be repeated a fixed and predetermined number of times. The general form of a for loop is as follows:
for v = expression
statements;
end
Here, expression is an expression, and statements are one or more lines of statements.
2. while Loop
A while loop evaluates a set of commands an indefinite number of times. The general form of a while loop is as follows:
while expression
statements;
end
As long as the elements in the expression expression are true, the command lines statements between while and end are executed.
3. continue Command
The continue command is often used with for or while statements. Its function is to end the current iteration of the loop, i.e., skip the statements in the loop body that have not yet been executed, and proceed to the next judgment of whether to execute the loop.
4. break Command
The break command is also often used with for or while statements. Its function is to terminate the current loop and jump out of the innermost loop. Using the break command allows you to exit the loop based on a condition without waiting for the loop to end naturally.
Error Control
The try...catch statement can perform error trapping. It places statements that might cause exceptions inside the try block. Thus, when a statement in the try block causes an exception, the catch block can catch it and handle it differently based on different error types. The syntax format of the try...catch command is:
try
statement,
...,
statement,
catch
statement,
...,
statement,
end
Program Termination Control
The return command allows the currently running function to exit normally and return to the function that called it to continue running. This statement is often used at the end of a function to end the function's execution normally.
The break, continue, and return functions are easily confused. Table 3-5 provides a detailed comparison of them.
Table 3-5: Differences Between break, continue, and return Functions
| Function | Used Where | Description |
|---|---|---|
| break | for or while loop | When encountered, exits the loop. In nested loops, goes to the adjacent outer loop. |
| continue | for or while loop | Skips remaining statements in the current loop iteration and proceeds to the next iteration. |
| return | Any location | When encountered, immediately exits the function and returns to the calling function. |