Break 和 Return 有什么区别?
What is the different between Break and Return?
我有两个 Matlab 代码,我想确定一个矩阵是否对称?我使用了嵌套 "for loops"。此外:
- 在第一个代码中,我必须退出程序两次
''break''。
- 在第二个代码中,我得到了命令''Return''来
程序退出。
Break和Return有什么问题吗?
我附上了我的代码。
第一个代码:
clc
clear all
A=input('Please enter your matrix=')
n=length(A);
temp=0;
for i=1:n-1
for j=i+1:n
if A(i,j)~=A(j,i)
temp=1;
break
end
end
if temp==1
disp('Matrix A is not symmetric.')
break
end
end
第二个代码:
clc
clear all
A=input('Please enter your matrix=')
n=length(A);
temp=0;
for i=1:n-1
for j=i+1:n
if A(i,j)~=A(j,i)
disp('Matrix A is not symmetric.')
return
end
end
end
否。使用都没有问题。这完全取决于目的。
break
终止 for
或 while
循环的执行。 break 语句之后的循环中的语句不执行。
在嵌套循环中,break 仅从它所在的循环中退出。控制传递给该循环结束后的语句。它在循环的外部块中保留控制权。
return
强制 MATLAB return 在调用函数到达函数末尾之前对其进行控制。调用函数是调用包含对 return 的调用的脚本或函数的函数。如果直接调用包含 return 的函数或脚本,则没有调用函数和 MATLAB return 对命令提示符的控制。
在条件块中,例如 if
或 switch
,或在循环控制语句中,例如 for
或 while
,一个 return
语句不只是退出循环;它退出脚本或函数并return控制调用函数或命令提示符。
在嵌套循环控件中,break
跳出它所在的循环并继续外层循环。
您发布的代码目前没有问题。这是对 MATLAB 自己的文档的一些详细说明,希望能为您解决一些问题。
根据文档,break
将跳出 for
或 while
循环:
break terminates the execution of a for or while loop. Statements in the loop after the break statement do not execute.
In nested loops, break exits only from the loop in which it occurs. Control passes to the statement that follows the end of that loop.
例如,下面的代码只会跳出最内层的循环。
for k = 1:3
fprintf('k = %d\n', k);
for m = 1:4
fprintf('m = %d\n', m);
% Will go back and evaluate the loop using the next k
break
end
end
这将打印
k = 1
m = 1
k = 2
m = 1
k = 3
m = 1
要跳出两个循环,您还需要在外循环中加入第二个break
。
for k = 1:3
fprintf('k = %d\n', k);
for m = 1:4
fprintf('m = %d\n', m);
% Will go back and evaluate the loop using the next k
break
end
% Continue executing code after the for loop
break
end
disp('This will still execute')
这将打印
k = 1
m = 1
This will still execute
另一方面,return
会跳出 函数 。
return forces MATLAB® to return control to the invoking function before it reaches the end of the function. The invoking function is the function that calls the script or function containing the call to return. If you call the function or script that contains return directly, there is no invoking function and MATLAB returns control to the command prompt.
这意味着 return
将完全退出函数(无论是否在循环内)。
function looper()
for k = 1:3
fprintf('k = %d\n', k);
for m = 1:4
fprintf('m = %d\n', m);
% No more iterations of ANY loop will be executed
return
end
end
disp('This will not execute')
end
这将打印
k = 1
m = 1
我有两个 Matlab 代码,我想确定一个矩阵是否对称?我使用了嵌套 "for loops"。此外:
- 在第一个代码中,我必须退出程序两次 ''break''。
- 在第二个代码中,我得到了命令''Return''来 程序退出。
Break和Return有什么问题吗?
我附上了我的代码。
第一个代码:
clc
clear all
A=input('Please enter your matrix=')
n=length(A);
temp=0;
for i=1:n-1
for j=i+1:n
if A(i,j)~=A(j,i)
temp=1;
break
end
end
if temp==1
disp('Matrix A is not symmetric.')
break
end
end
第二个代码:
clc
clear all
A=input('Please enter your matrix=')
n=length(A);
temp=0;
for i=1:n-1
for j=i+1:n
if A(i,j)~=A(j,i)
disp('Matrix A is not symmetric.')
return
end
end
end
否。使用都没有问题。这完全取决于目的。
break
终止 for
或 while
循环的执行。 break 语句之后的循环中的语句不执行。
在嵌套循环中,break 仅从它所在的循环中退出。控制传递给该循环结束后的语句。它在循环的外部块中保留控制权。
return
强制 MATLAB return 在调用函数到达函数末尾之前对其进行控制。调用函数是调用包含对 return 的调用的脚本或函数的函数。如果直接调用包含 return 的函数或脚本,则没有调用函数和 MATLAB return 对命令提示符的控制。
在条件块中,例如 if
或 switch
,或在循环控制语句中,例如 for
或 while
,一个 return
语句不只是退出循环;它退出脚本或函数并return控制调用函数或命令提示符。
在嵌套循环控件中,break
跳出它所在的循环并继续外层循环。
您发布的代码目前没有问题。这是对 MATLAB 自己的文档的一些详细说明,希望能为您解决一些问题。
根据文档,break
将跳出 for
或 while
循环:
break terminates the execution of a for or while loop. Statements in the loop after the break statement do not execute. In nested loops, break exits only from the loop in which it occurs. Control passes to the statement that follows the end of that loop.
例如,下面的代码只会跳出最内层的循环。
for k = 1:3
fprintf('k = %d\n', k);
for m = 1:4
fprintf('m = %d\n', m);
% Will go back and evaluate the loop using the next k
break
end
end
这将打印
k = 1
m = 1
k = 2
m = 1
k = 3
m = 1
要跳出两个循环,您还需要在外循环中加入第二个break
。
for k = 1:3
fprintf('k = %d\n', k);
for m = 1:4
fprintf('m = %d\n', m);
% Will go back and evaluate the loop using the next k
break
end
% Continue executing code after the for loop
break
end
disp('This will still execute')
这将打印
k = 1
m = 1
This will still execute
另一方面,return
会跳出 函数 。
return forces MATLAB® to return control to the invoking function before it reaches the end of the function. The invoking function is the function that calls the script or function containing the call to return. If you call the function or script that contains return directly, there is no invoking function and MATLAB returns control to the command prompt.
这意味着 return
将完全退出函数(无论是否在循环内)。
function looper()
for k = 1:3
fprintf('k = %d\n', k);
for m = 1:4
fprintf('m = %d\n', m);
% No more iterations of ANY loop will be executed
return
end
end
disp('This will not execute')
end
这将打印
k = 1
m = 1