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
Advertisements