for – while – switch
description
Transcript of for – while – switch
![Page 1: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/1.jpg)
1
for – while – switch
ผศ.ดร.อนั�นัต์ ผลเพิ่��มAnan Phonphoem
http://www.cpe.ku.ac.th/[email protected]
![Page 2: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/2.jpg)
2
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
![Page 3: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/3.jpg)
3
for loops
for loop var = m:s:n statementsend
Variable > nTrue
False
Increment variable by s
Set loop variable = m
Statements
![Page 4: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/4.jpg)
4
for loops (Example)
for a = 1:2:10 total = total + aend
a > 10True
False
a = a + 2
a = 1
total = total +a
![Page 5: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/5.jpg)
5
break command
To abnormally jump out of the loop before its end
![Page 6: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/6.jpg)
6
Find the answer of the program
for num = 10:-2:0 disp(num); temp = 2*num - 10; solution = temp + 5;endsolution = solution - 10
>> cmd1 10 8 6 4 2 0
solution = -15
Resultscmd1.m
![Page 7: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/7.jpg)
7
break example
% showing ‘break’ commandfor num = 10:-2:0 disp(num); temp = 2*num - 10; if (temp <= 0) break end solution = temp + 5;endsolution = solution - 10
>> cmd2 10 8 6 4
solution = -3
Resultscmd2.m
![Page 8: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/8.jpg)
8
continue command
To skip for some cases and continue execute the rest of the loop
For example, skip the error that might happen
![Page 9: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/9.jpg)
9
continue example
a = [100 0 10 -10];for k = 1:length(a) solution = 1000/a(k)end
>> cmd3solution = 10Warning: Divide by zero.> In cmd3 at 3solution = Infsolution = 100solution = -100
Resultscmd3.m
![Page 10: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/10.jpg)
10
continue example
% showing ‘continue’ commanda = [100 0 10 -10];for k = 1:length(a) if (a(k)== 0) continue end solution = 1000/a(k)end
>> cmd4solution = 10
solution = 100
solution = -100
Resultscmd4.m
![Page 11: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/11.jpg)
11
while loop
Looping process is terminated based on the specified condition
![Page 12: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/12.jpg)
12
conditionFalse
True
Statements
while loops
while condition statementsend
Set loop variable = m
Increment variableWithout the , no way to get out of the loop.
It’s called “infinite loop”
![Page 13: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/13.jpg)
13
while example (I)
while k > 0 disp(k);end
>> cmd5??? Undefined function or variable "k".
Error in ==> cmd5 at 1while k > 0
Results
cmd5.m
![Page 14: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/14.jpg)
14
while example (II)
k = 5;while k > 0 disp(k);end
>> cmd6 5 5 5 5 …
Results
cmd6.m
To break the programPress “Ctrl-C”
![Page 15: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/15.jpg)
15
while example (III)
k = 5;while k > 0 disp(k); k = k-1;end
>> cmd7 5 4 3 2 1
Resultscmd7.m
![Page 16: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/16.jpg)
16
while and for loop
% showing ‘break’ commandfor num = 10:-2:0 disp(num); temp = 2*num - 10; if (temp <= 0) break end solution = temp + 5;endsolution = solution - 10
cmd2.m Rewrite the programBy using “while”
![Page 17: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/17.jpg)
17
while and for loop
% using whilenum = 10;temp = 2*num - 10; while temp > 0 disp(num); solution = temp+5; temp = 2*num -10; num = num-2;endsolution = solution - 10
cmd8.m
![Page 18: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/18.jpg)
18
Nested Logic
condition1 FalseTrue
Statement1condition2
FalseTrue
Statement3Statement2
if logical exp1 statement g1elseif logical exp2 statement g2else statement g3end
![Page 19: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/19.jpg)
19
if-else and switch comparison
switch input expression case value1 statement g1 case value2 statement g2 otherwise statement g3end
if logical exp1 statement g1elseif logical exp2 statement g2else statement g3end
![Page 20: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/20.jpg)
20
Nested Logic – (Example)
a == 20 FalseTrue
c = a * 2 c == 30
FalseTrue
c = 0c = a + 2
if a == 20 c = a * 2elseif a == 30 c = a + 2else c = 0 end
switch a case 20 c = a * 2 case 30 c = a + 2 otherwise c = 0 end
![Page 21: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/21.jpg)
21
switch
switch input expression case value1 statement group 1 case value2 statement group 2 case value1 statement group 3 ... otherwise statement group nend
![Page 22: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/22.jpg)
22
switch example (I)
response = input(‘Type like, hate, or ok: ’,’s’);switch response 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
cmd9.m
![Page 23: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/23.jpg)
23
switch example (I)
>> cmd9Type like, hate, or ok: likeI really like it>> cmd9Type like, hate, or ok: hateI do not like it>> cmd9Type like, hate, or ok: abcYour enter is wrong >> cmd9Type like, hate, or ok: LikeYour enter is wrong
![Page 24: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/24.jpg)
24
Can you make it better ?
![Page 25: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/25.jpg)
25
while true response = input(‘Type like,hate,ok, or quit:’,’s’); response = lower(response); switch response 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 switchend %end while
switch example (II)cmd10.m
![Page 26: for – while – switch](https://reader035.fdocuments.us/reader035/viewer/2022062222/56814ba0550346895db87b3f/html5/thumbnails/26.jpg)
26
switch example (II)
>> cmd10Type like, hate, ok, or quit: likeI really like itType like, hate, ok, or quit: HateI do not like itType like, hate, ok, or quit: abcYour enter is wrongType like, hate, ok, or quit: quit>>