Control Flow Statement
Program control statement is divided into these four categories:
- Conditional Control: if, else, elseif, nested if-else, and switch.
- Loop Control: for, while, continue, break
- Error Control: try, catch
- Program Termination: return
Conditional Control Statement
This group of control statements enables to select at run-time, which block of code is executed. To make this selection based on whether a statement is true or false, use the if statement (which may include else or elseif). To select from several possible options depending on the value of an expression, use the switch and case statements.
MATLAB if
- The if is a conditional statement that provides the functionality to choose a block of code to execute at run time.
- A predefined condition is checked, and the remaining code executes based on the output of the condition.
- The if statement is defined by the if keyword.
- The control flows within the if block if the condition is found true.
Syntax:
Example:
Output:
a = 122
227 183
134 177
28 196
207 72
85 174
74 140
187 100
3 16
13 196
167 85
total hours of job done by you in a year are: 3085
Great...you earned a bonus amount of Rs. 10000
Following are the points while using if statement in MATLAB:
- Always close the if block with the end keyword.
- The elseif and else are optional, use them if there are more conditions to be checked.
- The elseif and else are an optional parts of the if statement and don't require additional end keyword.
- Remember not to use space in between else & if of the elseif keyword, because this leads to nested if statement and each if block needs to be close with the end keyword.
- There is no limit to use multiple elseif statements.
- We can nest the if statement by using it within another if statement.
MATLAB if-else
- If the first condition is not true, then we can define other statements to run by using the else keyword.
Syntax:
Example:
Output:
MATLAB if-elseif-else
- If we have more than one option or condition to check, then use elseif keyword.
Syntax:
Example:
Output:
MATLAB nested if-else
- If statements can be nested, but each if statement requires the end keyword.
Syntax:
Example 1:
Output:
enter a number greater than 1 and less than 10: 9
a = 9
the random number is: 14
the random number is not divisible by the entered number
Example 2:
MATLAB switch
The switch is another type of conditional statement and executes one of the group of several statements.
- If we want to test the equality against a pre-defined set of rules, then the switch statement can be an alternative of the if statement.
Syntax:
Following are the points while using switch in MATLAB:
Similar to if block, the switch block tests each case until one of the case_expression is true. It is evaluated as:
- case & switch must be equal for numbers as- case_expression == switch_expression.
- For character vectors, the result returned by the strcmp function must be equal to 1 as - strcmp(case_expression, switch_expression) == 1.
- For object, case_expression == switch_expression.
- For a cell array, at least one of the elements of the cell array in case_expression must match switch_expression.
- switch statement doesn't test for inequality, so a case_expression cannot include relational operators such as < or > for comparison against the switch_expression.
Example 1:
Example 2:
|