260 likes | 461 Views
for – while – switch. ผศ.ดร.อนันต์ ผลเพิ่ม Anan Phonphoem http://www.cpe.ku.ac.th/~anan anan@cpe.ku.ac.th. Loops. Repeating a calculation for a number of times Each repetition of the loop is a pass Two types of loops (In MATLAB) for loop while loop. Set loop variable = m.
E N D
for – while – switch ผศ.ดร.อนันต์ ผลเพิ่ม Anan Phonphoem http://www.cpe.ku.ac.th/~anan anan@cpe.ku.ac.th
Loops • Repeating a calculation for a number of times • Each repetition of the loop is a pass • Two types of loops (In MATLAB) • forloop • whileloop
Set loop variable = m for loop var = m:s:n statements end True Variable > n False Statements Increment variable by s for loops
for a = 1:2:10 total = total + a end for loops (Example) a = 1 True a > 10 False total = total +a a = a + 2
break command To abnormally jump out of the loop before its end
Find the answer of the program cmd1.m Results for num = 10:-2:0 disp(num); temp = 2*num - 10; solution = temp + 5; end solution = solution - 10 >> cmd1 10 8 6 4 2 0 solution = -15
break example cmd2.m Results % showing ‘break’ command for num = 10:-2:0 disp(num); temp = 2*num - 10; if (temp <= 0) break end solution = temp + 5; end solution = solution - 10 >> cmd2 10 8 6 4 solution = -3
continue command To skip for some cases and continue execute the rest of the loop For example, skip the error that might happen
continue example cmd3.m Results a = [100 0 10 -10]; for k = 1:length(a) solution = 1000/a(k) end >> cmd3 solution = 10 Warning: Divide by zero. > In cmd3 at 3 solution = Inf solution = 100 solution = -100
continue example cmd4.m Results % showing ‘continue’ command a = [100 0 10 -10]; for k = 1:length(a) if (a(k)== 0) continue end solution = 1000/a(k) end >> cmd4 solution = 10 solution = 100 solution = -100
while loop Looping process is terminated based on the specified condition
Set loop variable = m False condition True Statements Without the , no way to get out of the loop. It’s called “infinite loop” Increment variable while loops while condition statements end
while example (I) cmd5.m while k > 0 disp(k); end Results >> cmd5 ??? Undefined function or variable "k". Error in ==> cmd5 at 1 while k > 0
while example (II) cmd6.m k = 5; while k > 0 disp(k); end Results >> cmd6 5 5 5 5 … To break the program Press “Ctrl-C”
while example (III) cmd7.m Results k = 5; while k > 0 disp(k); k = k-1; end >> cmd7 5 4 3 2 1
while and for loop cmd2.m Rewrite the program By using “while” % showing ‘break’ command for num = 10:-2:0 disp(num); temp = 2*num - 10; if (temp <= 0) break end solution = temp + 5; end solution = solution - 10
while and for loop cmd8.m % using while num = 10; temp = 2*num - 10; while temp > 0 disp(num); solution = temp+5; temp = 2*num -10; num = num-2; end solution = solution - 10
condition1 False True condition2 True False Statement1 Statement2 Statement3 Nested Logic if logical exp1 statement g1 elseif logical exp2 statement g2 else statement g3 end
if-elseandswitchcomparison if logical exp1 statement g1 elseif logical exp2 statement g2 else statement g3 end switch input expression case value1 statement g1 case value2 statement g2 otherwise statement g3 end
a == 20 False True c == 30 c = a * 2 True False c = a + 2 c = 0 Nested Logic – (Example) if a == 20 c = a * 2 elseif a == 30 c = a + 2 else c = 0 end switch a case 20 c = a * 2 case 30 c = a + 2 otherwise c = 0 end
switch switch input expression case value1 statement group 1 case value2 statement group 2 case value1 statement group 3 ... otherwise statement group n end
switch example (I) cmd9.m response = input(‘Type like, hate, or ok: ’,’s’); switchresponse case‘like’ disp(‘I really like it’); case‘hate’ disp(‘I do not like it’); case ‘ok’ disp(‘It is ok’); otherwise disp(‘Your enter is wrong’); end
switch example (I) >> cmd9 Type like, hate, or ok: like I really like it >> cmd9 Type like, hate, or ok: hate I do not like it >> cmd9 Type like, hate, or ok: abc Your enter is wrong >> cmd9 Type like, hate, or ok: Like Your enter is wrong
switch example (II) cmd10.m while true response = input(‘Type like,hate,ok, or quit:’,’s’); response = lower(response); switchresponse case ‘like’ disp(‘I really like it’); case ‘hate’ disp(‘I do not like it’); case ‘ok’ disp(‘It is ok’); case ‘quit’ break otherwise disp(‘Your enter is wrong’); end %end switch end %end while
switch example (II) >> cmd10 Type like, hate, ok, or quit: like I really like it Type like, hate, ok, or quit: Hate I do not like it Type like, hate, ok, or quit: abc Your enter is wrong Type like, hate, ok, or quit: quit >>