6.Flow control
-
Upload
usablelabs -
Category
Education
-
view
4.138 -
download
0
description
Transcript of 6.Flow control
![Page 1: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/1.jpg)
6. Flow Control
Faculty of Technology and Environment
Prince of Songkla University, Phuket Campus
2/2551
976-140 Object-Oriented Programming
344-202 Introduction to OOP
การโปรแกรมเชงวตถ
![Page 2: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/2.jpg)
2Object-Oriented Programming 2/2551
Contents
Flow Control
If, if-else, nested-if
switch-case
while
do-while
for-next, Nested-for
Break
continue
![Page 3: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/3.jpg)
3Object-Oriented Programming 2/2551
Flow control
Start
Input
Process
Output
End
ในการท างานของโปรแกรมโดยทวไปจะ
ด าเนนงานตามล าดบค าสง
สวนใหญแลวตองมการตรวจสอบเงอนไขหรอ
ตดสนใจเพอด าเนนการ
Flow control คอการควบคมการท างานของ
โปรแกรมใหท างานตามเงอนไขทก าหนดไว
ค าสงเลอกกระท า ไดแก if-else, switch-case
ค าสงกระท าซ า ไดแก do-while, while, for
![Page 4: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/4.jpg)
4Object-Oriented Programming 2/2551
Flow control
การตดสนใจ จะดจากเงอนไขในการควบคม โดยใช boolean operator
ตรวจสอบวามคาความจรงเปนจรงหรอเทจกอน เพอตดสนใจวาจะท า
หรอไม
![Page 5: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/5.jpg)
5Object-Oriented Programming 2/2551
If
ใชส าหรบเลอกทศทางการท างาน ตามเงอนไข true หรอ false รปแบบท 1 : if
เชน
Condition Action 1true
false
![Page 6: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/6.jpg)
6Object-Oriented Programming 2/2551
If-else
Condition
Action 2
truefalse
Action 1
รปแบบท 2 : if…else…
เชน
![Page 7: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/7.jpg)
7Object-Oriented Programming 2/2551
If-else
ถาใน if หรอ else มค าสงเดยว ไมตองใสวงเลบปกกากได
![Page 8: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/8.jpg)
8Object-Oriented Programming 2/2551
Try this!
Edit this program to handle with input a=5, b=0
5
0
Exception in thread "main" java.lang.ArithmeticException: / by zeroat TestDemo.main(TestDemo.java:12)
5
0
Error: divided by zero!
![Page 9: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/9.jpg)
9Object-Oriented Programming 2/2551
Nested-If
รปแบบท 3 : if ซอน if
Note: If จะคกบ else ทอยใกลทสด
![Page 10: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/10.jpg)
10Object-Oriented Programming 2/2551
Try this
เขยนโปรแกรมรบคะแนนจากคยบอรด และตดพมพเกรดตามเงอนไข
ตอไปนโดยใช nested-if
ถา คะแนน 80 ขนไป ใหพมพ Good
ถา คะแนน 50 ขนไปและ นอยกวา 80 ใหพมพ Pass
ถา คะแนนนอยกวา 50 ใหเกรด Fail
![Page 11: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/11.jpg)
11Object-Oriented Programming 2/2551
Switch-case
ใชส าหรบเลอกทศทางการท างาน ตามคาของตวแปรทตรวจสอบ ชนดขอมลของตวแปรทตรวจสอบเปน char, byte, short, int เทานน
แตละ case ตองเปน literal หรอคาคงททสมพนธกนกบชนดขอมลนน
![Page 12: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/12.jpg)
12Object-Oriented Programming 2/2551
Switch-case
Case default อาจมหรอไมกได และอยต าแหนงใดกไดใน switch
แตละ case จะท างานจนกวาจะพบค าสง break และหยดตรวจสอบ case
ทเหลอ
ตวอยาง ก าหนดอตราดอกเบยเงนฝากประจ าจากจ านวนปทฝาก
ประจ า 3 ป ดอกเบย 5%
ประจ า 5 ป ดอกเบย 10%
ประจ า 10 ป ดอกเบย 15%
![Page 13: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/13.jpg)
13Object-Oriented Programming 2/2551
Switch-case
ตวอยาง การทหลาย case ใหท างานค าสงเดยวกน
ถา g = ‘B’
ผลลพธทไดคออะไร
![Page 14: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/14.jpg)
14Object-Oriented Programming 2/2551
Switch-case
หาก case นนไมมค าสง break จะท าค าสงใน case ถดไป
จงแสดงผลลพธเมอ g = ‘C’
![Page 15: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/15.jpg)
15Object-Oriented Programming 2/2551
While
ใชส าหรบการท างานซ า ๆ โดยตรวจสอบเงอนไขกอนวาเปนจรงจงจะท า และท าจนกวาเงอนไขนนจะเปนเทจ
สวนประกอบหลกของการท าซ า ก าหนดคาเรมตนทจะใชตรวจสอบ
ทดสอบเงอนไข และท าเมอเงอนไขจรง
ในลปตองมการเปลยนคาเพอท าใหเงอนไขเปนเทจเพอส นสดการวนลป
Condition false
true
Statements
![Page 16: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/16.jpg)
16Object-Oriented Programming 2/2551
While
Example ตองการพมพ ************* จ านวน 10 บรรทด
ก าหนด i=1 เพอใชนบรอบบรรทด
ตรวจสอบเงอนไขกอนจะท าในลป
ถายงท าไมครบ 10 รอบกจะพมพ ****
เปลยนคา i เพอใชตรวจสอบวา
จะท ารอบถดไปหรอไม
![Page 17: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/17.jpg)
17Object-Oriented Programming 2/2551
Try this!
int x = 5;int sum = 0;i = 0;while(i <= x){sum = sum + x;i++;
}System.out.println(sum);
int i = 10;
while(i > 0)
{
System.out.print(i + " ");
i--;
}
![Page 18: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/18.jpg)
18Object-Oriented Programming 2/2551
Do-while
Example
int count = 0;
do {
System.out.println( count );
count++ ;
} while ( count < 10 );
Condition false
true
Statements
Do-while จะท างานอยางนอย 1 ครงกอนทจะท าการตรวจสอบเงอนไข
หากเงอนไขเปนจรงจะท าในลปถดไป และจะท าจนกวาเงอนไขเปนเทจ
![Page 19: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/19.jpg)
19Object-Oriented Programming 2/2551
Try this!
int y = 10;
do
{
System.out.println(y);
y += 10;
} while(y <= 100);
จงเขยนโปรแกรมแสดงเลขสมทมคา 1-10 จ านวน 10 ตว
โดยใช while loop
![Page 20: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/20.jpg)
20Object-Oriented Programming 2/2551
For
ใชส าหรบการท างานซ า ๆ โดยจ านวนรอบจะตรวจสอบตวแปรทก าหนดเปนตวนบรอบ
ตวนบรอบจะตองถกก าหนดคาเรมตนให
แตละรอบจะมการปรบคาตวนบรอบ และตรวจสอบวาตวนบรอบตรง
ตามเงอนไขหรอไม
หากเงอนไขเปนจรง จะท าในลป
หากเงอนไขเปนเทจ จะออกจากลป
Test
Initial
Statements
Change
true
false
![Page 21: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/21.jpg)
21Object-Oriented Programming 2/2551
For
Example
for (int i = 0; i < 10; i=i+2) {
System.out.println(i);
}
for (int i = 0; i < 5; i++) {
System.out.println(i++);
}
![Page 22: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/22.jpg)
22Object-Oriented Programming 2/2551
Nested For
การใช For ซอน For จะมการวนรอบโดยแตละรอบของ for ดานนอก จะม
การท างาน for ดานใน
For-loop นอก ท างาน 5 รอบ โดยเพมคา i รอบละ 1
แตละรอบของ i จะท างานใน for-loop ใน โดยคา j เพมรอบละ 1 และท าจ านวน
รอบตามเงอนไข
![Page 23: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/23.jpg)
23Object-Oriented Programming 2/2551
Nested For
![Page 24: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/24.jpg)
24Object-Oriented Programming 2/2551
การตรวจสอบเงอนไขดวยเครองหมาย ==
ถาเปรยบเทยบ primitive data type ดวยเครองหมาย == จะเปนการ
เปรยบเทยบวาคาเทากนหรอไม
![Page 25: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/25.jpg)
25Object-Oriented Programming 2/2551
การตรวจสอบเงอนไขดวยเครองหมาย ==
ถาเปรยบเทยบคาของ object ซงเปน reference data type ดวย
เครองหมาย == จะมความหมายวาเปน object เดยวกนหรอไม
![Page 26: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/26.jpg)
26Object-Oriented Programming 2/2551
การเปรยบเทยบ String
String เปน reference data type
การเปรยบเทยบดวย == หมายถงการเปรยบเทยบวาเปน object เดยวกนหรอไม
หากตองการเปรยบเทยบคา ใชเมธอด boolean equals(String s)
รปแบบ stringObject1.equals(stringObject2)
http://java.sun.com/j2se/1.4.2/docs/api/javax/naming/Reference.html
![Page 27: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/27.jpg)
27Object-Oriented Programming 2/2551
การเปรยบเทยบ String
What is the result?
![Page 28: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/28.jpg)
28Object-Oriented Programming 2/2551
Keyword break
ในลป while, do-while, for ถาในลปมค าสง break อย หากโปรแกรม
ท างานไปเจอ break จะหลดออกจากลปทนท
![Page 29: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/29.jpg)
29Object-Oriented Programming 2/2551
Keyword continue
ในลป while, do-while, for หากโปรแกรมท างานไปเจอ continue จะ
ท าในลปในรอบถดไปทนท
![Page 30: 6.Flow control](https://reader034.fdocuments.net/reader034/viewer/2022052315/5566a63dd8b42acd288b4f8c/html5/thumbnails/30.jpg)
30Object-Oriented Programming 2/2551
จด note แบบอจฉรยะ