Matlab Tutor6 Break Continue
Matlab Tutor6 Break Continue
The break statement terminate the execution of a for loop or while loop. When a break
statement is encountered, execution proceeds with the next statement outside of the
loop. In nested loops, break exists from the innermost loop only.
Syntax:
1. break
Output:
a = 6×6
82 17 70 54 54 10
27 18 70 66 33 27
60 43 64 41 11 16
3 10 4 82 62 29
43 60 7 72 78 45
32 48 32 97 43 53
program running smoothly
program running smoothly
program encounters the number 27, which is not useful for the current
program;
at index no.:2
so loop terminates now.....bye bye
Example2:
Output:
Output:
a = 4×4
0.2398 -1.6118 0.8617 0.5812
-0.6904 -0.0245 0.0012 -2.1924
-0.6516 -1.9488 -0.0708 -2.3193
1.1921 1.0205 -2.4863 0.0799
negative number :-0.69036,found at index: 2,hence the program terminated
Example:
The system is installed at different locations all over the world. Somewhere the
temperature is measured in Celsius and somewhere it is measured in Fahrenheit. So we
need to take care of these temperature units also.
The continue statement works within a for or while loop and passes control to the next
iteration of the loop.
Syntax:
1. Continue
o Continue statement passes the control of the execution to the next iteration of
a for or while loop.
o All remaining statements following the continue statement do not execute for the
current iteration.
o The continue statement applies only to the body of the loop where it is called, hence in
nested loops, it affects the execution of the loop in which it occurs.
o The continue statement only works inside a for or while loop, and it can't be used inside
a function. But if a function is having a for or while loop, there we can
use continue inside the loop.
Flowdiagram of Continue Statement
Example1:
1. % program to print all numbers divisible by 3 and skip remaining
2. a = (1:4:50); % creates row vector from 1 to 50 with a step of 4
3. for k = 1:numel(a)
4. if rem(a(k),3)
5. continue
6. end
7. disp(a(k))
8. end
Output:
21
33
45
Syntax:
1. try
2. Statements
3. catch exception
4. Statements
5. end
Statements between try and catch execute first. If no error appears in executing
statements between try and catch, MATLAB further executes the statements/code after
the end keyword. If an error occurs during the execution of statements between try and
catch, MATLAB executes statements between catch and end. Try-catch statement can be
explained with the help of the following example.
Example:
1. a = ones(4);
2. b = zeros(3);
3. try
4. c = [a;b];
5. catch ME
6. disp(ME)
7. end
Output:
identifier: 'MATLAB:catenate:dimensionMismatch'
message: 'Dimensions of arrays being concatenated are not consistent.'
cause: {0×1 cell}
stack: [3×1 struct]
Correction: []
Following are the points while using a try/catch statement in MATLAB:
o The control of the program execution first enters the try block and executes each
statement.
o If any error occurs during the execution, then the control immediately passes to
the catch block, leaving any other statements of the try block unexecuted.
o If there is no error occurs inside try block, then the control doesn't enter the
catch block. The control then reaches to the statements after the end keyword of
try/ catch block.
o Whenever any error or exception occurs in the try block, the MATLAB constructs
an instance of the MException class and returns the object in the catch
statement.
o The MException class object can be accessed with the variable ME.
o The MException class object has five properties-identifier, message, stack,
cause, and Correction. These properties describe details about the occurred
exception.
o We can't use multiple catch block, only one catch block within a try block is
allowed.
o We can use nested try/catch blocks if required.
1. a = ones(4);
2. b = zeros(3);
3. try
4. c = [a;b];
5. catch ME
6. % strcmp string compare function, to check the same string in ME identifier
7. if (strcmp(ME.identifier,'MATLAB:catenate:dimensionMismatch'))
8. % if it is true then change the message property
9. msg = ['dimension mismatch occured: First argument has ',...
10. num2str(size(a,2)), ' columns, while second argument has ',...
11. num2str(size(b,2)),' columns.'];
12. causeException = MException('MATLAB:mycode:dimensions',msg)
13. ME = addCause(ME, causeException);
14. end
15.
16. end
Output:
causeException =
MException with properties:
identifier: 'MATLAB:mycode:dimensions'
message: 'dimension mismatch occured: First argument has 4 columns,
while second argument has 3 columns.'
cause: {}
stack: [0×1 struct]
Correction: []
Program Termination
Program termination control allows to exit from our program at some point before its
normal termination point.
MATLAB return
The command return simply return the control to the invoking function.
Example:
MATLAB end
The end keyword in MATLAB serves two main purposes:
Output:
a = 4×4
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
......
0 1 1 1
0 1 1 1
0 1 1 1
0 1 1 1
...end
1. a = randi(100,4,4)
2. b = a(end,2:end) % here first end argument indicates the last row,
3. %and the second indicates the columns number from 2 to the last
4. a = 4×4
5. 43 66 68 66
6. 92 4 76 18
7. 80 85 75 71
8. 96 94 40 4
9. b = 1×3
10. 94 40 4