控制循环退出的关键字Break、Continue和Return
for i = 1:2
for j = 2:4
disp(["i = " + num2str(i) + ", j = " + num2str(j)]);
end
end
for i = 1: 2
for j = 1 : 4
if(j == 2)
continue; % 结束当前循环(continue后面的语句不再执行)
end
disp(["i = " + num2str(i) + ",j = " + num2str(j)]);
end
end
for i = 1:2
for j = 1 : 100
if(j == 2)
break ; %退出整层循环(一层)
end
disp(["i = " + num2str(i) + ", j = " + num2str(j)]);
end
end
for i = 1:2
for j = 1:3
if(j == 2)
return;%退出多层循环
end
disp(["i = " + num2str(i) + ", j = " + num2str(j)]);
end
end
% Ctrl + i:智能缩进
for k = 1:5
for i = 1:2
for j = 1:3
if(j == 2)
return; % 退出多层循环
end
disp(["k = " + num2str(k)+ " , i = " + num2str(i)+ ", j = " + num2str(j)]);
end
end
end
2021年8月24日笔记