
- MATLAB - Home
- MATLAB - Overview
- MATLAB - Features
- MATLAB - Environment Setup
- MATLAB - Editors
- MATLAB - Online
- MATLAB - Workspace
- MATLAB - Syntax
- MATLAB - Variables
- MATLAB - Commands
- MATLAB - Data Types
- MATLAB - Operators
- MATLAB - Dates and Time
- MATLAB - Numbers
- MATLAB - Random Numbers
- MATLAB - Strings and Characters
- MATLAB - Text Formatting
- MATLAB - Timetables
- MATLAB - M-Files
- MATLAB - Colon Notation
- MATLAB - Data Import
- MATLAB - Data Output
- MATLAB - Normalize Data
- MATLAB - Predefined Variables
- MATLAB - Decision Making
- MATLAB - Decisions
- MATLAB - If End Statement
- MATLAB - If Else Statement
- MATLAB - If…Elseif Else Statement
- MATLAB - Nest If Statememt
- MATLAB - Switch Statement
- MATLAB - Nested Switch
- MATLAB - Loops
- MATLAB - Loops
- MATLAB - For Loop
- MATLAB - While Loop
- MATLAB - Nested Loops
- MATLAB - Break Statement
- MATLAB - Continue Statement
- MATLAB - End Statement
- MATLAB - Arrays
- MATLAB - Arrays
- MATLAB - Vectors
- MATLAB - Transpose Operator
- MATLAB - Array Indexing
- MATLAB - Multi-Dimensional Array
- MATLAB - Compatible Arrays
- MATLAB - Categorical Arrays
- MATLAB - Cell Arrays
- MATLAB - Matrix
- MATLAB - Sparse Matrix
- MATLAB - Tables
- MATLAB - Structures
- MATLAB - Array Multiplication
- MATLAB - Array Division
- MATLAB - Array Functions
- MATLAB - Functions
- MATLAB - Functions
- MATLAB - Function Arguments
- MATLAB - Anonymous Functions
- MATLAB - Nested Functions
- MATLAB - Return Statement
- MATLAB - Void Function
- MATLAB - Local Functions
- MATLAB - Global Variables
- MATLAB - Function Handles
- MATLAB - Filter Function
- MATLAB - Factorial
- MATLAB - Private Functions
- MATLAB - Sub-functions
- MATLAB - Recursive Functions
- MATLAB - Function Precedence Order
- MATLAB - Map Function
- MATLAB - Mean Function
- MATLAB - End Function
- MATLAB - Error Handling
- MATLAB - Error Handling
- MATLAB - Try...Catch statement
- MATLAB - Debugging
- MATLAB - Plotting
- MATLAB - Plotting
- MATLAB - Plot Arrays
- MATLAB - Plot Vectors
- MATLAB - Bar Graph
- MATLAB - Histograms
- MATLAB - Graphics
- MATLAB - 2D Line Plot
- MATLAB - 3D Plots
- MATLAB - Formatting a Plot
- MATLAB - Logarithmic Axes Plots
- MATLAB - Plotting Error Bars
- MATLAB - Plot a 3D Contour
- MATLAB - Polar Plots
- MATLAB - Scatter Plots
- MATLAB - Plot Expression or Function
- MATLAB - Draw Rectangle
- MATLAB - Plot Spectrogram
- MATLAB - Plot Mesh Surface
- MATLAB - Plot Sine Wave
- MATLAB - Interpolation
- MATLAB - Interpolation
- MATLAB - Linear Interpolation
- MATLAB - 2D Array Interpolation
- MATLAB - 3D Array Interpolation
- MATLAB - Polynomials
- MATLAB - Polynomials
- MATLAB - Polynomial Addition
- MATLAB - Polynomial Multiplication
- MATLAB - Polynomial Division
- MATLAB - Derivatives of Polynomials
- MATLAB - Transformation
- MATLAB - Transforms
- MATLAB - Laplace Transform
- MATLAB - Laplacian Filter
- MATLAB - Laplacian of Gaussian Filter
- MATLAB - Inverse Fourier transform
- MATLAB - Fourier Transform
- MATLAB - Fast Fourier Transform
- MATLAB - 2-D Inverse Cosine Transform
- MATLAB - Add Legend to Axes
- MATLAB - Object Oriented
- MATLAB - Object Oriented Programming
- MATLAB - Classes and Object
- MATLAB - Functions Overloading
- MATLAB - Operator Overloading
- MATLAB - User-Defined Classes
- MATLAB - Copy Objects
- MATLAB - Algebra
- MATLAB - Linear Algebra
- MATLAB - Gauss Elimination
- MATLAB - Gauss-Jordan Elimination
- MATLAB - Reduced Row Echelon Form
- MATLAB - Eigenvalues and Eigenvectors
- MATLAB - Integration
- MATLAB - Integration
- MATLAB - Double Integral
- MATLAB - Trapezoidal Rule
- MATLAB - Simpson's Rule
- MATLAB - Miscellenous
- MATLAB - Calculus
- MATLAB - Differential
- MATLAB - Inverse of Matrix
- MATLAB - GNU Octave
- MATLAB - Simulink
MATLAB - End Statement
The end is a keyword that is used to terminate the block of code. The end keyword can be used inside for-loop, while-loop, if-statement, switch-statement, try and parfor statements.
The end statement is paired with the closest unpaired for , if , while, try, switch, parfor statement.
Let us see the syntax of the end statement in each of the loops and statements we mentioned above.
Syntax of End Statement in For Loop
for variable = startValue : endValue % Code to be executed in each iteration end
In a for-loop, the "end" statement is used to mark the end of the loop block, indicating where the loop's code execution should stop.
Syntax of End Statement Inside Nested for-loop
for outer_loop_variable = outer_loop_start:outer_loop_end % Outer loop code block for inner_loop_variable = inner_loop_start:inner_loop_end % Inner loop code block end % Code after the inner loop end
In a nested for-loop, the "end" statement is used to indicate the end of the inner loop's code block, providing the necessary closure for the nested structure. A nested for-loop consists of one loop inside another, allowing you to perform repetitive tasks with more complex iterations.
Syntax of End Statement Inside while-loop
while condition % Code to be executed end
In a while-loop, the "end" statement is used to mark the end of the loop block, defining the scope of code that will be executed repeatedly as long as the specified condition remains true.
Syntax of End Statement Inside Nested while-loop
while outer_loop_condition % Outer loop code block while inner_loop_condition % Inner loop code block end % Code after the inner loop end
In a nested while-loop, the "end" statement is used to indicate the end of the inner loop's code block and to define the boundaries of both the inner and outer loops.
Syntax of End Statement Inside if-statement
if condition % code to be executed if the condition is true end
In an if-statement, the "end" statement is used to indicate the end of the code block that should be executed when the specified condition is true.
Syntax of End Statement Inside Nested if-statement
if condition1 % Code to execute if condition1 is true if condition2 % Code to execute if condition2 is true else % Code to execute if condition2 is false end else % Code to execute if condition1 is false if condition3 % Code to execute if condition3 is true else % Code to execute if condition3 is false end end
In a nested if-statement, the "end" statement is used to mark the end of each if-else block, defining the boundaries of the nested structure.
Syntax of End Inside Switch Statement
switch expression case caseExpression1 % Code to be executed for case 1 case caseExpression2 % Code to be executed for case 2 ... case caseExpressionN % Code to be executed for case N otherwise % Code to be executed if none of the cases match end
In a switch statement, the "end" statement is used to mark the end of the entire switch block, indicating the boundary of the switch statement.
Syntax of End Statement Inside Nested Switch Statement
switch outerExpression case outerCase1 % Outer case 1 code switch innerExpression case innerCase1 % Inner case 1 code case innerCase2 % Inner case 2 code ... end case outerCase2 % Outer case 2 code switch innerExpression case innerCase1 % Inner case 1 code case innerCase2 % Inner case 2 code ... end ... end
In a nested switch statement, the "end" statement is used to mark the end of both the inner and outer switch blocks, defining the boundaries of the nested structure.
Syntax of End Statement Inside Try Statement
try statements catch exception statements end
In a try-catch statement, the "end" statement is used to mark the end of the "try" block, indicating the boundary of the code where potential exceptions are being handled.
Syntax of End Statement Inside Parallel for loop also Called as Parfor
parfor loopvar = initval:endval; statements; end
In a parallel for loop, also known as "parfor" loop, the "end" statement is used to mark the end of the loop block, just like in regular for-loops or other control structures.
Let us try out a few examples to see the involvement of the end statement.
Example 1
Here we are going to make use of the for-loop and the end statement in it.
sum = 0; for i = 1:5 sum = sum + i; end disp(sum);
The loop calculates the sum of the first five numbers.
The "end" statement defines the scope of the for loop and ensures that the code inside the loop block is executed repeatedly for each iteration of the loop variable "i."
On execution in matlab the output is −
>> sum = 0; for i = 1:5 sum = sum + i; end disp(sum); 15
Example 2
In this example will see the working of the end statement inside try/catch blocks.
try x = 10 / 0; catch disp("An error occurred: Division by zero"); end
In the above example the "try" block tries the division operation "x = 10 / 0," which is a division by zero, which throws an error. When MATLAB encounters the division by zero error within the try block, it immediately jumps to the "catch" block to handle the exception.The "catch" block is executed when an error occurs within the try block. In this case, it simply displays the message "An error occurred: Division by zero" using "disp".The "end" statement following the "catch" block marks the end of the catch block, ensuring that the code execution continues after the try-catch structure.
On execution in matlab the output is
>> try x = 10 / 0; catch disp("An error occurred: Division by zero"); end
Example 3
In this example will make use of parfor parallel for loop with end statement.
parfor i = 1:5 result(i) = i * 2; end disp(result);
The "parfor" loop is using the "parfor" keyword, indicating that the loop iterations can be executed in parallel. The loop variable "i" takes on values from 1 to 5 as specified by the range "1:5". The "end" statement marks the end of the parfor loop block, defining the scope of code where each iteration can be executed independently and potentially in parallel.
Inside the loop block, each iteration calculates the result for the current index "i" and stores it in the "result" array at the corresponding index.The resulting array "result" will be [2, 4, 6, 8, 10] containing the calculated values for each iteration.
On execution in matlab the output is −
>> parfor i = 1:5 result(i) = i * 2; end disp(result); 2 4 6 8 10