多个条件执行相同语句时,可以写在一个 case表达式中,如: switch (a) case{1357} disp (the value is odd') case{2468} disp (the value is even') otherwise disp (the value is out of range') end 16
16 多个条件执行相同语句时,可以写在一个 case表达式中,如: switch (a) case {1 3 5 7 } disp (‘the value is odd’) case {2 4 6 8 } disp (‘the value is even’) otherwise disp (‘the value is out of range’) end
eg.使用switch.结构判断学生成绩的等级,90分以上为 优,80~90为良,70≈80为中,60≈70为及格,60分以下 为不及格。 score=98; s1=fix(score/10); %取十位数 ■ switch s1 case 9,10} S=优 case 8 $=良' S= 优 case 7 S=中' case 6 ■ S=及格 otherwise ■ S=不及格 17 end
17 eg. 使用switch结构判断学生成绩的等级,90分以上为 优,80~90为良,70~80为中,60~70为及格,60分以下 为不及格。 score=98; s1=fix(score/10); %取十位数 switch s1 case {9,10} s='优' case 8 s='良' case 7 s='中' case 6 s='及格' otherwise s='不及格' end s = 优
迎 2.3.4其他控制流 1、continue语句 continue语句用于终止当前的循环操 作,并直接跳转到该循环的开始再次执行 该循环。 18
18 2.3.4 其他控制流 1、continue语句 continue语句用于终止当前的循环操 作,并直接跳转到该循环的开始再次执行 该循环
迎 eg.for中的continue语句 for i=1:5 结果: ifi==3: i=1 ■ continue; i=2 ■ end i=4 fprintf('i=%d n',i); i=5 ■ End of loop! end disp('End of loop!"); 19
19 eg. for 中的continue 语句 for i = 1:5 if i == 3; continue; end fprintf('i = %d\n',i); end disp('End of loop!'); 结果: i = 1 i = 2 i = 4 i = 5 End of loop!
② 2、break语句 break语句用于终止一个循环操作并立 即跳出该循环,以执行该循环体之后的程 序,break一般与if语句结合使用。 continue语句与break不同的是continue 只结束本次for或while循环,而继续进行下 次循环,continue一般也与if语句结合使 用。 20
20 2、break语句 break语句用于终止一个循环操作并立 即跳出该循环,以执行该循环体之后的程 序,break一般与if语句结合使用。 continue语句与break不同的是continue 只结束本次for或while循环,而继续进行下 次循环,continue一般也与if语句结合使 用