branching and looping statements charturong.ee.engr.tu.ac.th/cn208
DESCRIPTION
Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208. จาตุรงค์ ตันติบัณฑิต ภาควิชาวิศวกรรมไฟฟ้าและคอมพิวเตอร์ มหาวิทยาลัยธรรมศาสตร์. เอกสารประกอบการสอนนี้จัดทำโดย ดร.ทรงยศ นาคอริยกุล. Introduction. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/1.jpg)
Branching and Looping Statements
charturong.ee.engr.tu.ac.th/CN208
จาตรงค ตนตบณฑตภาควชาวศวกรรมไฟฟาและคอมพวเตอร
มหาวทยาลยธรรมศาสตร
เอกสารประกอบการสอนนจดทำาโดย ดร.ทรงยศ นาคอรยกล
![Page 2: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/2.jpg)
2
Introduction
ในบทน เราจะแนะนำาการเขยนโปรแกรมใน MATLAB โดยการใชคำาสงทควบคมการลำาดบการประมวลผลของคำาสงใน MATLAB โดยทแบงออกไดเปน 2 ประเภทใหญๆคอ คำาสง branches ทเลอกบางสวนของโคด (code) ไปประมวลผล และ คำาสง loops ประมวลผลโคดแบบวนซำา
โดยทเราจะพดถงหวขอตอไปน• การเขยนโปรแกรมโดยใช top-down design techniques• การใชขอมลประเภทตรรกะ (logical data type)• การใชคำาสง branches• การใชคำาสง loops
![Page 3: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/3.jpg)
3
การเขยนโปรแกรมแบบ top-down design
การเขยนโปรแกรมแบบ top-down design คอการแตกปญหาใหญทเราไดรบใหเปนหวขอยอยๆซงงายตอการแกไขและเขาใจ จากนนจงเขยนโปรแกรมแกปญหายอยๆใหสำาเรจและตรวจสอบวาโปรแกรมเหลานทำางานไดจรง ในขนตอนสดทายจงนำาโปรแกรมยอยๆเหลานมารวมกนเพอแกปญหาใหญทเราไดรบ
ขนตอนในการเขยนโปรแกรมแบบมประสทธผลสามารถทำาไดดงตอไปน• เขาใจปญหาทจะแกไขใหถกตองและชดเจน• กำาหนดตวอนพต (input) และตวเอาทพต (output) ของโปรแกรมให
ชดเจน• ออกแบบการแกไขปญหาใหละเอยด (design the algorithm)• เปลยนอลกอรทมใหเปนคำาสงใน MATLAB• ตรวจสอบและแกไขโปรแกรม
![Page 4: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/4.jpg)
4
การเขยนโคดเทยม (pseudocode)
ในการออกแบบโปรแกรมแตละครง ผเขยนโปรแกรมสมควรเขยน pseudocode เพอชวยใหงายตอการแกไขปญหา pseudocode ทเขยนจะเปนคำาพดภาษาองกฤษผสมกบรปแบบการใชใน MATLAB เชน เขยนคำาสงเปนบรรทดๆแบบ MATLAB โดยทคำาสงเปนคำาพดภาษาองกฤษ เปนตน
Pseudocode จะชวยผเขยนโปรแกรมในการเรยบเรยงความคดกอนทจะแปลงความคดเปนคำาสงใน MATLAB
![Page 5: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/5.jpg)
5
ตวอยางท 1: Temperature Conversion
จงเขยนโปรแกรม MATLAB ทเปลยนคาอณหภมในระบบฟาเรนไฮตเปนระบบองศาเซลเซยส โดยทโปรแกรมจะรบคาอนพตจากผใชโปรแกรมเปนตวเลขอณหภมในระบบฟาเรนไฮต แลวแสดงผลเปนอณหภมระบบองศาเซลเซยส ออกบนหนาจอ
![Page 6: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/6.jpg)
6
การแกไขปญหาเราจะแกไขปญหาแบบ top-down design ดงทกลาวไปแลวในขางตน
สำาหรบตวอยางนมขนาดเลกจงไมจำาเปนตองแตกปญหาเปนปญหายอยๆแตอยางใด โปรแกรมนสรปไดสนๆวามตวอนพตเปนตวเลขหนงตวและตวเอาทพตหนงตว ซงการออกแบบอลกอรทมในการแกปญหากแสดงไดดวยสมการตอไปน
เราสามารถเขยน pseudocode สำาหรบปญหานไดดงนPrompt user to enter temperature in degree FahrenheitRead temperature in degrees Fahrenheit (temp_f)temp_c in Celsius (temp_f – 32) / 1.8Write temperature in Celsius
( ) ( ) 32 /1.8T in Celsius T in Fahrenheit
![Page 7: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/7.jpg)
7
temp_conversion.m% Script file: temp_conversion% Programmer: …………….% Purpose: to convert an input temperature from degrees Fahrenheit% to an output temperature in Celsius.% Define variables:% temp_f -- Temperature in degrees Fahrenheit% temp_c -- Temperature in degrees Celsius
% Prompt the user for the input temperaturetemp_f = input('Enter the temperature in degrees Fahrenheit:');
% Convert to Celsius.temp_c = (temp_f - 32) / 1.8;
% Write out the result.fprintf('%6.2f degrees Fahrenheit = %6.2f Celsius.\n', temp_f, temp_c);
![Page 8: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/8.jpg)
8
Testing the program
หลงจากเขยนโปรแกรมสำาเรจแลว ผเขยนโปรแกรมสมควรทจะตองตรวจสอบความถกตองของโปรแกรมทเขยนเสมอ โดยทำาไดดวยการทดลองใสคาอนพตเพอตรวจสอบ
![Page 9: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/9.jpg)
9
The Logical Data Type
ชนดของขอมลอกประเภทหนงใน MATLAB คอ ขอมลตรรกะ (logical data type) ทมคาได 2 ชนดคอ จรง (true) หรอ เทจ (false) โดยมการเรมใชใน MATLAB 6.5 เปนตนไป ขอมลตรรกะสามารถถกสรางโดยคำาสง 2 ประเภทคอ relational operators กบ logic operators
หากเราตองการกำาหนดคาตวแปรใหเปนประเภท logical กทำาไดเชน>> a1 = true;
เวลาทขอมลประเภทตรรกะถกใชผสมกบตวเลข ขอมลประเภทตรรกะจะถกเปลยนเปนตวเลขโดยทคา true คอ 1 และคา false เปน 0 ถาหากขอมลตวเลขถกใชแทนทขอมลตรรกะ คาตวเลขทไมเทากบ 0 จะถกเปลยนเปน true และคา 0 จะถกเปลยนเปน false
![Page 10: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/10.jpg)
10
Relational Operators
คาโอเปอเรเตอรสำาหรบเปรยบเทยบความสมพนธระหวางคาตวแปรชนดสเกลารหรอเมทรกซกได โดยทคาโอเปอเรอเตอรทใชมดงน
โอเปอเรเตอร Relational คำาอธบาย==~=<>
<=>=
เทากบไมเทากบนอยกวามากกวานอยกวาหรอเทากบมากกวาหรอเทากบ
![Page 11: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/11.jpg)
11
ตวอยางการใช relational operators
โอเปอเรเตอร ผลลพธ3 < 4
3 == 43 ~= 43 <= 4
7+ 3 > 4 +103^2 >= 4
จรง (1) เทจ (0) จรง (1) จรง (1) เทจ (0) จรง (1)
![Page 12: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/12.jpg)
12
Logic Operators
โอเปอเรเตอร logic เปนโอเปอเรเตอรทใชเชอมความสมพนธระหวางคาของตวแปรทเกดขนโดยโอเปอเรเตอร relational ถาความสมพนธสอดคลองกนผลลพธทไดกจะมคาเปนจรง (true) แตถาความสมพนธไมสอดคลองกนผลลพธทไดกจะมคาเปนเทจ (false) โอเปอเรเตอร logic ทใชทวไปใน MATLAB มอย 3 แบบ คอ
โอเปอเรเตอร logic คำาอธบาย&|~
และ (AND)หรอ (OR)ไม (NOT)
![Page 13: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/13.jpg)
13
ผลลพธของโอเปอเรเตอร logic
ยกตวอยางเชน >> x = (5 > 3) & (2 < 9);
ผลลพธ x จะมคาเปนจรง (true)
อนพต และ (AND) หรอ (OR) ไม (NOT)
P Q false false false true true false true true
P & Q P | Q ~P false false true false true false true false true true
![Page 14: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/14.jpg)
14
Branchesการเขยนคำาสงทดสอบเงอนไขใน MATLAB มความจำาเปนอยางมาก เพอ
ชวยในการตดสนใจทซบซอน และชวยใหโปรแกรมตดสนใจเลอกคำาสงทจะกระทำาและคำาสงทจะไมกระทำา รปแบบการใชทนาสนใจคอ คำาสง if และคำาสง switch
คำาสง ifคำาสง if ใชทำาการทดสอบเงอนไขความสมพนธวาเปนจรงหรอเปนเทจแลวจงกระทำาคำาสงทอยภายใตเงอนไขนน มรปแบบการใชงานดงนif expression (command 1)(command 2)…end
ถาผลการทดสอบเงอนไขเปนจรง โปรแกรมMATLAB จะกระทำาคำาสง commands ทงหมดทอยระหวาง if และ end แตถาผลการทดสอบเปนเทจ โปรแกรมจะขาม(ไมประมวลผล) คำาสงทงหมดทอยระหวาง if และ end
![Page 15: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/15.jpg)
15
คำาสง if กบ else คำาสง if ยงมรปแบบการใชงานในการตดสนใจทซบซอน โดยมรปแบบการ
ใชงานรวมกบ else ดงน
if expression (commands evaluated if expression is true)
else(commands evaluated if expression is false)
end
ถาผลการทดสอบเงอนไขใน expression เปนจรง MATLAB จะกระทำาคำาสง commands ทงหมด (ซงอาจจะมมากกวา 1 คำาสง) ทอยระหวาง if กบ else แตถาหากผลการทดสอบ expression เปนเทจ คำาสง commands ทงหมดทอยระหวาง else กบ end จะถกกระทำา
![Page 16: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/16.jpg)
16
elseifหากในการตดสนใจมหลายเงอนไขและซบซอนมากขน คำาสง if มการใชรวมกบคำาสง
elseif ไดดงนif expression_1(commands_1 evaluated if expression_1 is true)elseif expression_2(commands_2 evaluated if expression_2 is true)elseif expression_3(commands_3 evaluated if expression_3 is true) else(commands evaluated if no other expression is true)end
ซงหากผลการทดสอบเงอนไขใน expression_1 เปนจรง คำาสง commands_1 ทงหมด (ซงอาจจะมมากกวา 1 คำาสง) จะถกกระทำาและจะขามคำาสงทเหลอทงหมดไปจนถง end แตถา expression_1 เปนเทจ expression_2 จะถกตรวจสอบเปนลำาดบตอไปเรอยๆ
ขอควรจำา: คำาสง else ตวสดทายและ commands ทเกยวกบ else นนอาจจะมหรอไมมกได
![Page 17: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/17.jpg)
17
ตวอยางท 2: function of two variables
จงเขยนโปรแกรม MATLAB เพอชวยคำานวณคาของฟงกชน f(x,y) โดยกำาหนดใหผใชเปนผกำาหนดคา x และ y แลวแสดงผล f(x,y) ออกบนหนาจอ ฟงกชน f(x,y) มดงคาตอไปน
f(x,y) = x + y, x >= 0 and y >= 0 x + y2, x >= 0 and y < 0 x2 + y, x < 0 and y >= 0 x2 + y2, x < 0 and y < 0
![Page 18: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/18.jpg)
18
การแกไขปญหาเราเรมดวยการเขยน pseudocode สำาหรบปญหาขอน
Prompt the user for the values x and yRead x and y if x >= 0 and y >= 0
func x + yelseif x >= 0 and y < 0
func x + y^2elseif x < 0 and y >= 0
func x^2 + yelse
func x^2 + y^2endWrite out f(x,y)
![Page 19: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/19.jpg)
19
func_xy.m% Script file: func_xy.m% Programmer: ...................% Purpose: This program solves the function f(x,y) for a user-% specified x and y, where f(x, y) is defined as:% f(x,y) = x + y, x >= 0 and y >= 0% x + y2, x >= 0 and y < 0% x2 + y, x < 0 and y >= 0% x2 + y2, x < 0 and y < 0 %% Define variables:% x -- first independent variable % y -- second independent variable% func -- resulting function
![Page 20: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/20.jpg)
20
% Prompt the user for the values x and yx = input('Enter the x coefficient: ');y = input('Enter the y coefficient: ');
% Calculate the function f(x,y) if x >= 0 & y >= 0
func = x + y;elseif x >= 0 & y < 0
func = x + y^2;elseif x < 0 & y >= 0
func = x^2 + y;else
func = x^2 + y^2;end
%Write the value of the functiondisp (['The value of the function is ', num2str(func)]);
![Page 21: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/21.jpg)
21
คำาสง switchคำาสง switch เปนคำาสงประเภท branching อกแบบหนงทใหผเขยน
โปรแกรมเลอกใช รปแบบของคำาสง switch มดงนswitch switch_var
case case_expression_1(commands_1)
case case_expression_2(commands_2)
otherwise(commands_3)
end
ถาคาของ switch_var เทากบ case_expression_1 คำาสง commands_1 ทงหมด (ซงอาจมมากกวา 1 คำาสง) จะถกกระทำาแลวโปรแกรมจะขามคำาสงทเหลอทงหมดไปจนถง end แตถา switch_var เทากบ case_expression_2 commands_2 ทงหมดจะถกกระทำาแทน แบบนตอไปเรอยๆ ถาทก case เปนเทจ คำาสงใน otherwise จะถกใช
ขอควรจำา: คำาสง otherwise จะมหรอไมมกได
![Page 22: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/22.jpg)
22
การรวม case ใน switchถา case บาง case ใหผลลพธของคำาสงเดยวกน เราสามารถรวม case
เหลานเขาดวยกนได ซงทำาไดโดยการรวมไวในเครองหมายปกกา (brackets) โดยใชรปแบบดงน
switch switch_varcase {case_expression_1, case_expression_2}
(commands_1)case {case_expression_3, case_expression_4}
(commands_2)otherwise
(commands_3)end
โดยท switch_var สามารถเปนตวเลข (double) หรอตวอกษร (character string) กได
![Page 23: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/23.jpg)
23
ตวอยางการใชคำาสง switchหากเราตองการแสดงผลบนหนาจอวาตวเลขทผใชเลอกระหวาง 1 ถง 10
เปนเลขคหรอเลขคกสามารถใชคำาสง switch ไดดงน
value = input('Choose a integer between 1 and 10: ');switch value
case {1, 3, 5, 7, 9}disp('The number is odd.');
case {2, 4, 6, 8, 10}disp('The number is even.');
otherwisedisp('The number is out of range.');
end
![Page 24: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/24.jpg)
24
ตวอยางคำาสง switch กบตวแปร charคำาสง switch สามารถใชเปรยบเทยบตวแปรประเภทตวอกษร (character
string) โดยเชนการแปลงคาตวแปร x จากเซนตมเตอรเปนระบบอนเชน เปนนว ฟต หรอ เมตร
x = 2.7;units = 'm' ;switch unitscase {'inch' , 'in' }y = x/2.54;case {'feet' , 'ft' }y = x/2.54/12;case {'meter' , 'm' }y = x/100;otherwisedisp(['Unknown Units: ', units])y = nan;end
หลงจาก run โปรแกรมนแลว ผลลพธทไดคอ y = 0.027
![Page 25: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/25.jpg)
25
คำาสงวนลปการเขยนโปรแกรมทซบซอน ในหลายๆครงมความจำาเปนตองคำานวณชด
คำาสงบางอยางซำาไปซำามาหลายๆรอบ ซงคำาสงวนลปทสำาคญๆมอย 2 แบบคอ คำาสง for และคำาสง while
ขอแตกตางทสำาคญของ 2 คำาสงนคอ คำาสง while จะวนซำาไปเรอยๆไมมทสนสด จนกวา การทดสอบเงอนไขใน expression จะเปนเทจ ในขณะท คำาสง for จะกำาหนดจำานวนรอบการวนซำาไวชดเจน
![Page 26: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/26.jpg)
26
คำาสง while
คำาสง while จะทำาการทดสอบเงอนไขใน expression ทกๆรอบของการวนซำา ถาผลการทดสอบใน expression ใหคาเปนจรง โปรแกรมจะกระทำาคำาสงทงหมดภายใน while ลปหนงรอบ แลวกลบมาตรวจสอบ expression อกครงหนง และจะทำาแบบนตอไปเรอยๆ จนกวาผลการทดสอบจะเปนเทจ แลวMATLAB จงจะกระทำาคำาสงหลงจากคำาสง end
คำาสง while มรปแบบการใชงานดงน
while expression(commands)
end
![Page 27: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/27.jpg)
27
ตวอยางการใชคำาสง while
ในตวอยางนจะแสดงการใช while loop ในการถามผใชโปรแกรมใหตอบคำาถามทางคณตศาสตร และจะไมออกจากโปรแกรมจนกวาผใชโปรแกรมจะตอบถก
sum = input('What is five plus ten?: ');while (sum ~= 15)
disp('Nope! Your answer is incorrect.');sum = input('What is five plus ten?: ');
enddisp('Great! You are smart.')
![Page 28: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/28.jpg)
28
คำาสง for
คำาสง for จะกระทำาคำาสงทงหมด (ซงอาจจะมมากกวา 1 คำาสง) ทอยระหวาง expression กบ end ในจำานวนรอบทคงท ทกำาหนดไวใน expression โดยมรปแบบการใชงานดงน
for index = array(commands)
endตวแปร index คอ ตวแปรของลป (loop variable) และ array คอ อาเรย
ทเปนตวควบคมจำานวนลป (loop control expression) แตละคอลมนใน array จะถกกำาหนดใหกบตวแปร index ในแตละรอบในการวนลป คำาสง commands ทงหมดจะถกกระทำาในแตละจำานวนรอบของการวนลป โดยสวนมากแลว array จะเปนเวกเตอรและมแบบฟอรมคอ first:incr:last
![Page 29: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/29.jpg)
29
ตวอยางการใชคำาสง for
sum = 0;for ii = 1:10
sum = sum + ii;fprintf('sum = %6.2f.\n', sum);
endfprintf('The value of ii is now %d.\n', ii);
![Page 30: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/30.jpg)
30
คำาอธบายตวอยางการใช for1 .ในตอนเรมตนของคำาสง for โปรแกรม MATLAB สราง array ซง
เปน row vector มคาจาก 1 ถง 102. ในครงท 1 ของการวนลป ตวแปร ii มคาเปน 1 ซงเปนคาเลขตวแรก
ใน array และคำาสงทงหมดระหวาง คำาสง for กบ end จะถกกระทำา ผลลพธคอคาของตวแปร sum = sum + ii หรอ 0 + 1 ซงกคอ 1
3. เมอคำาสง fprintf ถกกระทำา โปรแกรมจะเจอกบคำาสง end ดงนนโปรแกรมจะวนกลบไปยงจดเรมตนของคำาสง for และกำาหนดคาตวเลขตวตอไปใน array ใหกบตวแปร ii ซงกคอเลข 2 เพราะฉะนนในการวนลปครงน ii = 2 และคาของตวแปร sum = sum + ii = 1 + 2 หรอ 3
4. โปรแกรม MATLAB จะทำาคำาสงในขอ 3 ไปเรอยๆ จนกระทงตวเลขตวสดทายใน array ถกกำาหนดใหกบตวแปร ii และคำาสง commands ระหวาง for กบ end จะถกกระทำาเปนรอบสดทาย
![Page 31: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/31.jpg)
31
ตวอยางท 2 ของการใชคำาสง for
sum = 0;for ii = 1:2:9
sum = sum + 1;fprintf('sum = %6.2f.\n', sum);
endfprintf('The value of ii is now %d.\n', ii);
![Page 32: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/32.jpg)
32
คำาเตอนในการใชคำาสง forหลกเลยงการกำาหนดคาใหมใหกบตวแปร index โดยใชคำาสง commands ภายในคำา
สง for ลปการกำาหนดคาใหมใหกบตวแปร index ภายในคำาสง for ไมสามารถทำาใหคำาสงวนลป
for จบเรวขนได และอาจทำาใหเกดขอผดพลาดหรอ bug ไดงาย ยกตวอยางเชนsum = 0;for ii = 1:10ii = 5;sum = sum + ii;fprintf('sum = %6.2f.\n', sum);endfprintf('The value of ii is now %d.\n', ii);
ในตวอยางน คำาสง for จะวนลปเปนจำานวน 10 รอบและคา sum จะมคาเปน 50 เมอจบโปรแกรม
![Page 33: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/33.jpg)
33
คำาสง break กบ continue
มคำาสง 2 คำาสงทสามารถใชควบคมการวนลปของคำาสง while และ for ไดนนกคอ คำาสง breakและ continue
คำาสง break หยดการทำางานของคำาสงวนลปใน for หรอ while และสงให MATLAB กระทำาคำาสงบรรทดตอไปจากคำาสง end
คำาสง continue หยดการทำางานของคำาสงวนลปใน for หรอ while แลวสงให MATLAB กลบไปทำาคำาสงในจดเรมตนของคำาสงวนลป
![Page 34: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/34.jpg)
34
ตวอยางการใช breakfor ii = 1:5if ii == 3 break;endfprintf('ii = %d\n', ii);enddisp(['End of loop!']);
เวลาท m-file นถก run ผลลพธทจะไดบนหนาจอคอ ii = 1ii = 2End of loop!
นนเพราะเมอ ii เทากบ 3 เงอนไขทดสอบใน if จะเปนจรงแลวคำาสง break จะถกกระทำา ซงทำาใหขามคำาสงทงหมดในคำาสง for ลป แลวไปกระทำาคำาสงหลงจาก end
![Page 35: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/35.jpg)
35
ตวอยางการใชคำาสง continuefor ii = 1:5if ii == 3 continue;endfprintf('ii = %d\n', ii);enddisp(['End of loop!']);
เวลาท m-file นถก run ผลลพธทจะไดบนหนาจอคอ ii = 1ii = 2ii = 4ii = 5End of loop!
นนคอเมอ ii เทากบ 3 เงอนไขทดสอบใน if จะเปนจรงแลวคำาสง continue จะถกกระทำา ซงทำาใหขามคำาสงทงหมดในรอบลปนน ไปยงคำาสง for ใหม แลว ii จะมคาเปน 4 ในการวนรอบใหมและกระทำาคำาสงตอไปจนจบ
![Page 36: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/36.jpg)
36
Nested Loops
ในบางกรณ อาจมการเขยนคำาสงวนลปภายในของอกคำาสงวนลป ซงทง 2 ลปนนเรยกวา nested loops โดยตวอยางตอไปนเปน คำาสง for ลปภายในอกคำาสง for ลป (two nested for loops) สำาหรบหาผลคณของจำานวนเตมของจำานวน
for ii = 1:3 for jj = 1:3 product = ii * jj;
fprintf('%d * %d = %d\n', ii, jj, product); endend
![Page 37: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/37.jpg)
37
คำาสง for ตวนอก(ตวแรก) จะกำาหนดคา 1 ใหกบตวแปร ii จากนนคำาสง for ตวใน(ตวทสอง) จะถกกระทำา 3 ครงโดยทตวแปร jj จะมคา 1, 2, และ 3 ซงหลงจากจบการกระทำา for ลปตวในแลว คำาสง for ตวนอกจะกำาหนดคาตวแปร ii เปน 2 แลว เรมทำาคำาสง for ตวในใหมอกครงแบบน ไปจนกระทงคำาสง for ตวนอก ไดกำาหนดคาใหกบตวแปร ii จนครบ 3 ครง
ผลลพธทไดจากโปรแกรมขางตนคอ 1 * 1 = 11 * 2 = 21 * 3 = 3 2 * 1 = 22 * 2 = 42 * 3 = 63 * 1 = 33 * 2 = 63 * 3 = 9
![Page 38: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/38.jpg)
38
การใชคำาสง break หรอ continue ใน nested loops
ถาคำาสง break หรอ continue ปรากฎอยภายในคำาสงวนลปตวใน คำาสง break หรอ continue นนจะมผลตอคำาสงวนลปตวในเทานน ยกตวอยางเชน
for ii = 1:3 for jj = 1:3 if jj == 3
break;endproduct = ii * jj;fprintf('%d * %d = %d\n', ii, jj, product);
end fprintf('End of inner loop\n');end fprintf('End of outer loop\n');
![Page 39: Branching and Looping Statements charturong.ee.engr.tu.ac.th/CN208](https://reader036.vdocuments.mx/reader036/viewer/2022062302/5681566b550346895dc41faa/html5/thumbnails/39.jpg)
39
เมอคำาสงวนลปตวใน กำาหนดคาให jj เปน 3 คำาสง break จะถกกระทำาซงทำาใหโปรแกรมหยดการกระทำาคำาสงลปตวใน และพมพแสดงผล End of inner loop จากนนโปรแกรมกจะกลบไปทคำาสงวนลปตวนอก และเพมคา ii อก 1 และกระทำาคำาสงวนลปตวในอกครง ผลลพธทไดคอ
1 * 1 = 11 * 2 = 2End of inner loop2 * 1 = 22 * 2 = 4
End of inner loop3 * 1 = 33 * 2 = 6End of inner loopEnd of outer loop