MATLAB - end 語句



end 是一個用於終止程式碼塊的關鍵字。end 關鍵字可以在 for 迴圈、while 迴圈、if 語句、switch 語句、try 和 parfor 語句中使用。

end 語句與最近的未配對的 for、if、while、try、switch、parfor 語句配對。

讓我們看看上面提到的每個迴圈和語句中 end 語句的語法。

for 迴圈中 end 語句的語法

for variable = startValue : endValue
   % Code to be executed in each iteration
end

在 for 迴圈中,"end" 語句用於標記迴圈塊的結束,指示迴圈程式碼執行應停止的位置。

巢狀 for 迴圈內 end 語句的語法

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

在巢狀 for 迴圈中,"end" 語句用於指示內部迴圈程式碼塊的結束,為巢狀結構提供必要的閉包。巢狀 for 迴圈包含一個迴圈在另一個迴圈內,允許您執行具有更復雜迭代的重複任務。

while 迴圈內 end 語句的語法

while condition
   % Code to be executed
end

在 while 迴圈中,"end" 語句用於標記迴圈塊的結束,定義只要指定條件為真就會重複執行的程式碼範圍。

巢狀 while 迴圈內 end 語句的語法

while outer_loop_condition
   % Outer loop code block
   
   while inner_loop_condition
      % Inner loop code block        
   end
   
   % Code after the inner loop
  
end

在巢狀 while 迴圈中,"end" 語句用於指示內部迴圈程式碼塊的結束,並定義內部和外部迴圈的邊界。

if 語句內 end 語句的語法

if condition
   % code to be executed if the condition is true
end

在 if 語句中,"end" 語句用於指示當指定條件為真時應執行的程式碼塊的結束。

巢狀 if 語句內 end 語句的語法

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

在巢狀 if 語句中,"end" 語句用於標記每個 if-else 塊的結束,定義巢狀結構的邊界。

switch 語句內 end 的語法

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

在 switch 語句中,"end" 語句用於標記整個 switch 塊的結束,指示 switch 語句的邊界。

巢狀 switch 語句內 end 語句的語法

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

在巢狀 switch 語句中,"end" 語句用於標記內部和外部 switch 塊的結束,定義巢狀結構的邊界。

try 語句內 end 語句的語法

try
   statements
catch exception
   statements
end

在 try-catch 語句中,"end" 語句用於標記 "try" 塊的結束,指示正在處理潛在異常的程式碼的邊界。

並行 for 迴圈(也稱為 parfor)內 end 語句的語法

parfor loopvar = initval:endval; statements; end

在並行 for 迴圈(也稱為 "parfor" 迴圈)中,"end" 語句用於標記迴圈塊的結束,就像在常規 for 迴圈或其他控制結構中一樣。

讓我們嘗試一些示例來了解 end 語句的作用。

示例 1

這裡我們將使用 for 迴圈和其中的 end 語句。

sum = 0;
for i = 1:5
   sum = sum + i; 
end
disp(sum);

迴圈計算前五個數字的總和。

"end" 語句定義了 for 迴圈的範圍,並確保迴圈塊內的程式碼對於迴圈變數 "i" 的每次迭代都重複執行。

在 matlab 中執行後,輸出為 -

>> sum = 0;
for i = 1:5
   sum = sum + i; 
end
disp(sum);

   15

示例 2

在這個例子中,我們將看到 try/catch 塊內 end 語句的工作原理。

try
   x = 10 / 0; 
catch
   disp("An error occurred: Division by zero");
end

在上面的例子中,"try" 塊嘗試除法運算 "x = 10 / 0",這是除以零,這會丟擲一個錯誤。當 MATLAB 在 try 塊中遇到除以零錯誤時,它會立即跳轉到 "catch" 塊以處理異常。"catch" 塊在 try 塊中發生錯誤時執行。在這種情況下,它只是使用 "disp" 顯示訊息 "發生錯誤:除以零"。"catch" 塊後面的 "end" 語句標記了 catch 塊的結束,確保程式碼執行在 try-catch 結構之後繼續。

在 matlab 中執行後,輸出為

>> try
   x = 10 / 0; 
catch
   disp("An error occurred: Division by zero");
end

示例 3

在這個例子中,我們將使用帶 end 語句的 parfor 並行 for 迴圈。

parfor i = 1:5
   result(i) = i * 2;
end
disp(result);

"parfor" 迴圈使用 "parfor" 關鍵字,表示迴圈迭代可以並行執行。迴圈變數 "i" 從 1 到 5 取值,如範圍 "1:5" 所指定。"end" 語句標記了 parfor 迴圈塊的結束,定義了每個迭代可以獨立執行且可能並行執行的程式碼範圍。

在迴圈塊內,每個迭代都會計算當前索引 "i" 的結果,並將其儲存在對應索引處的 "result" 陣列中。生成的陣列 "result" 將為 [2, 4, 6, 8, 10],包含每個迭代的計算值。

在 matlab 中執行後,輸出為 -

>> parfor i = 1:5
   result(i) = i * 2;
end
disp(result);

   2     4     6     8    10
廣告
© . All rights reserved.