# Matlab中的continue、break和return语句

Continue:

eg, 在for 循环中：

clc
clear all
close all

i = 1;
COND  = 1;

for i = 1:20
if mod(i,2)==0
continue
else

fprintf('the array is %d\n',i)
end
end


the array is 1
the array is 3
the array is 5
the array is 7
the array is 9
the array is 11
the array is 13
the array is 15
the array is 17
the array is 19


eg, 在while 循环中：

clc
clear all
close all

i = 1;
COND  = 1;

while COND ==1

if mod(i,2)==0
i = i+1;
continue;
else
fprintf('the array is %d\n',i)
end

if i>=19
COND = 2;
end
i = i+1;
end


the array is 1
the array is 3
the array is 5
the array is 7
the array is 9
the array is 11
the array is 13
the array is 15
the array is 17
the array is 19


break:

break和continue用法相似，区别在于，一旦执行了break, 就会推出循环，而不是进入下一个循环：

clc
clear all
close all

i = 1;
COND  = 1;

for i = 1:10
if mod(i,3)==0
break
else

fprintf('the array is %d\n',i)
end
end


the array is 1
the array is 2


%%
clc
clear all
close all

i = 1;
COND  = 1;

for j = 1:4

for i = 1:j
if mod(i,3)==0
break
else

fprintf('the array is %d\n',i)
end
end
end


the array is 1
the array is 1
the array is 2
the array is 1
the array is 2
the array is 1
the array is 2


return:
return 跳出函数，返回调用函数处。

%%
clc
clear all
close all

i = 1;
COND  = 1;

for i = 1:20
if mod(i,2)==0
return
else

fprintf('the array is %d\n',i)
end
end
%%
clc
clear all
close all

i = 1;
COND  = 1;

while COND ==1

if mod(i,2)==0
i = i+1;
return;
else
fprintf('the array is %d\n',i)
end

if i>=19
COND = 2;
end
i = i+1;
end


the array is 1
>>


clc
clear all
close all
flag = 20 ;
if flag <-10
fprintf('y<-10\n')
return
elseif flag >20
fprintf('y>20\n')
return
elseif flag >=-10 && flag <20
fprintf('-10<=y<20\n')
return
else fprintf('y=20\n')
return
end


break       跳出循环；
continue    继续循环，（不执行 循环体内continue 后面的语句，直接进行下一循环）
return      跳出函数，返回调用函数 处。


clc
clear all
close all

i = 1;
COND  = 1;

for i = 1:20
if mod(i,2)==0
continue
else

fprintf('the array is %d\n',i)
end
end
%%
clc
clear all
close all

i = 1;
COND  = 1;

while COND ==1

if mod(i,2)==0
i = i+1;
continue;
else
fprintf('the array is %d\n',i)
end

if i>=19
COND = 2;
end
i = i+1;
end
%%
clc
clear all
close all

i = 1;
COND  = 1;

for i = 1:4
if mod(i,3)==0
break
else

fprintf('the array is %d\n',i)
end
end

%%
clc
clear all
close all

i = 1;
COND  = 1;

for j = 1:4

for i = 1:j
if mod(i,3)==0
break
else

fprintf('the array is %d\n',i)
end
end
end
%%
clc
clear all
close all

i = 1;
COND  = 1;

for i = 1:20
if mod(i,2)==0
return
else

fprintf('the array is %d\n',i)
end
end
%%
clc
clear all
close all

i = 1;
COND  = 1;

while COND ==1

if mod(i,2)==0
i = i+1;
return;
else
fprintf('the array is %d\n',i)
end

if i>=19
COND = 2;
end
i = i+1;
end
%%
clc
clear all
close all
flag = 20  ;
if flag <-10
fprintf('y<-10\n')
return
elseif flag >20
fprintf('y>20\n')
return
elseif flag >=-10 && flag <20
fprintf('-10<=y<20\n')
return
else fprintf('y=20\n')
return
end



06-01 4万+
04-22 2万+
09-05 3973
04-09 1289
07-18 4万+
05-31 1915
10-17 1万+
03-12 1127
06-22 9478
11-18 3万+
12-05 5533
05-16 2万+
12-27 1228
11-05
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie