งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

ครั้งที่ 3.

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "ครั้งที่ 3."— ใบสำเนางานนำเสนอ:

1 ครั้งที่ 3

2 โครงสร้างควบคุมการทำงาน
( Control Structures) Control Structures : Decisions and Loops วันนี้ฝึกปฏิบัติและงานเก็บคะแนน 5 คะแนน

3 Control flows in Java is the same as in C and C++. if-else switch for
Control Statements Control flows in Java is the same as in C and C++. if-else switch for while do-while

4 Control Structure Sequential Selection Loop IF… IF…ELSE… IF…IF… SWITCH
WHILE.. DO…WHILE FOR

5 Control Structures statement1; statement2; ... statementn; }
compound statement or block { statement1; statement2; ... statementn; }

6 Boolean Expressions The simplest boolean expression is a boolean variable, whose type is the primitive type boolean just two possible values: true and false logical operations: and, or, not Ex. of variable declarations: boolean eligible; boolean flag; boolean leapYear = true;

7 Relational operators Relational operators: used to compare two expressions which are type compatible Operator Meaning Example Value < less than pressure < MaxPressure true <= less than or equal to kTemp <= kIce true > greater than init > ‘K’ false >= greater than or equal to >= kTemp true == equals init == ‘Q’ false != does not equal kIce != kTemp true The variables used in the examples above have the following types and values: kIce kTemp init J pressure MaxPressure 38.5

8 Logical operators Logical operators: and &&, or ||, not !
operand1 operand operand1 && operand2 operand1 || operand2 false false false false false true false true true false false true true true true true operand ! operand false true true false

9 Control flows in Java is the same as in C and C++. if-else switch for
Control Statements Control flows in Java is the same as in C and C++. if-else switch for while do-while

10 if statement statement if ( count > 0 ) { average = sum / count;
Syntax: if statement with one consequent if (condition) statement true condition false statement if ( count > 0 ) { average = sum / count; System.out.println (average); }

11 import javax.swing.JOptionPane;
class If1 { public static void main (String args[]) { int score = 50; if (score > 0) { String message = "score = " + score + "grade = pass" ; JOptionPane.showMessageDialog(null,message); System.exit(0); }

12 if statement if ( count > 0 ) average = sum / count; else
Syntax: if statement with two alternatives (if-else) if (condition) statementT else statementF true false condition statementT statementF if ( count > 0 ) average = sum / count; else average = 0.0; System.out.println (average);

13 Nested if-else and switch statements
Simple if-else format if (condition) statementT else statementF Nested if -else format if (condition1 ) if (condition2 ) statementT2 else statementF2 if (condition3 ) statementT3 statementF3 Matching rule: in a nested if-else statement, an else is matched with the closest preceding if that is not matched yet. if ( x > 0 ) if (y > 0) { z = sqrt (x) + sqrt (y); System.out.println(“z = “ + z); } else System.out.println( “*** can’t compute z”);

14 Multiple-alternative selection
Multiple-alternative selection: choose exactly one alternative out of more than two mutually-exclusive alternatives. Implementation: either as a nested if-else structure (where new decisions fall on else branches), or as an equivalent if-else if structure. . Nested if -else format if (condition1 ) statement1 else if (condition2 ) statement2 if (condition3 ) statement3 if (conditionn ) statementn statementn+1 Equivalent if -else if format if (condition1 ) statement1 else if (condition2 ) statement2 else if (condition3 ) statement3 else if else if (conditionn ) statementn else statementn+1 .

15 Example of multiple-alternative selection
3 7 12 pH neutral acidic very acidic alkaline very alkaline Solution pH: true pH < 3 if (pH < 3) System.out.println( "Solution is very acidic.”); else if (pH < 7) System.out.println( "Solution is acidic.”); else if (pH == 7) System.out.println(”Solution is neutral."); else if (pH < 12) System.out.println( "Solution is alkaline.”); else System.out.println( "Solution is very alkaline.”); false very acidic pH>=3 true pH<7 false acidic pH>=7 true pH ==7 false neutral pH >7 true if (pH >= 12) System.out.println( "Solution is very alkaline.”); else if (pH > 7) System.out.println( "Solution is alkaline.”); else if (pH == 7) System.out.println(”Solution is neutral."); else if (pH >= 3) System.out.println( "Solution is acidic.”); else System.out.println( "Solution is very acidic.”); pH<12 false alkaline pH>=12 very alkaline

16 import javax.swing.JOptionPane;
class If2 { public static void main (String args[]) { double score ;String message,data; data = JOptionPane.showInputDialog(null,"Enter score : "); score = Double.parseDouble(data); if (score >= 49) message = "score = " + score + "grade = Pass"; else message = "score = " + score + "grade = F" ; JOptionPane.showMessageDialog(null,message); System.exit(0); }

17 Example if-else class IfElseDemo {
public static void main(String[] args) { int testscore = 76; char grade; if (testscore >= 90 ) { grade = 'A'; } else if (testscore >= 80 ) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; } System.out.println(“Grade = “ + grade);

18 import javax.swing.JOptionPane;
class If22 { public static void main (String [ ]args) { int score ;String message,data; data = JOptionPane.showInputDialog(null,"Enter score : "); score = Integer.parseInt(data); if (score >= 49) message = "score = " + score + "grade = Pass"; else message = "score = " + score + "grade = F" ;

19 System.out.println("score = "+score); }
JOptionPane.showMessageDialog(null,message); System.exit(0); System.out.println("score = "+score); }

20 ฝึกปฏิบัติ ข้อ1. ให้เขียนโปรแกรมชื่อ work1_1.java แก้ปัญหาดังนี้
ต้องการคำนวณความสัมพันธ์ระหว่างน้ำหนักและส่วนสูงของบุคคลโดยรับค่าส่วนสูงของบุคคล รับรหัสเพศ(1=ชาย,2=หญิง) เข้ามาจากนั้นคำนวณหาน้ำหนักที่เหมาะสม โดยถ้าเป็น ชาย คำนวณ น้ำหนักที่เหมาะสม = ส่วนสูง – 100 หญิง คำนวณ น้ำหนักที่เหมาะสม = ส่วนสูง – 110 จากนั้นให้แสดง ข้อความ เพศ (Mชาย หรือFหญิง) ส่วนสูง และน้ำหนัก ออกทางจอภาพ (ทำหน้าเครื่องของตนเอง เสร็จแล้ว run พร้อมส่งเอกสารให้อาจารย์ในชั่วโมง)

21 Switch statement Interpretation: Switch statement syntax:
switch (selector ) { case label11: case label12: . . . case label1k: statements1 break; . case labeln1: case labeln2: case labelnm: statementsn default: statementsd } Interpretation: value of selector is evaluated and compared to each label in turn when a matching label is found, the statements that follow that label are executed until a break or a return statement is reached; all the other statements in the switch block are skipped if no matching label is found, the statements following default are executed. default clause is optional; if it’s missing, no statement is executed in the cases when no matching label was found. if a break (return) statement is missing, execution “falls through” to the statements associated with the next set of labels. Type of selector and of labels must be an ordinal type (in Java, ordinal types are: integer types, bool and char) cannot be a floating point type, nor a string of characters each label must be a unique value of the same type as the selector

22 คำสั่ง switch (The switch Statement)
มักจะถูกใช้บ่อยในกรณีของการเลือกหนึ่งทางเลือกในหลายๆทางเลือก ข้อจำกัดของการใช้คำสั่ง Switch ตัวแปรที่ใช้ในการตรวจสอบ จะต้องมีชนิดเป็นตัวเลขอย่างใดอย่างหนึ่ง ได้แก่ char, byte, short หรือ int Switch: switch (ตัวแปร) { case ค่าที่ 1 : คำสั่งที่ 1; break; case ค่าที่ 2 : คำสั่งที่ 2; . case ค่าที่ N : คำสั่งที่ N; default : คำสั่งเมื่อไม่มีค่าที่ตรงกับที่ระบุใน case ; }

23 Switch statement examples
// Set price for a light bulb of // , 60, 75, 100 or 150 watts switch (watts) { case 40: price = 0.50; break; case 60: price = 0.69; case 75: price = 0.85; case 100: case 150: price = 1; default: price = 0; System.out.print (“No bulb ” +watts + “watts in stock”); } // end switch if (watts == 40) price = 0.50; else if (watts== 60) price = 0.69; else if (watts == 75) price = 0.85; else if (watts == 100 || watts==150) price = 1; else { price = 0; System.out.print (“No bulb ”+watts + “watts in stock”); } same effect

24 public class SwitchDemo {
public static void main(String[] args) { int month = 8; switch (month) { case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; case 4: System.out.println("April"); break; case 5: System.out.println("May"); break; case 6: System.out.println("June"); break; case 7: System.out.println("July"); break; case 8: System.out.println("August"); break; case 9: System.out.println("September"); break; case 10: System.out.println("October"); break; case 11: System.out.println("November"); break; case 12: System.out.println("December"); break; }

25 public static void main(String[] args) { int day = 7; switch (day) {
Example switch class SwitchDemo2 { public static void main(String[] args) { int day = 7; switch (day) { case 1: System.out.println(“Monday"); break; case 2: System.out.println(“Tuesday"); break; case 3: System.out.println(“Wednesday"); break; case 4: System.out.println(“Thursday"); break; case 5: System.out.println(“Friday"); break; case 6: System.out.println(“Saturday"); break; case 7: System.out.println(“Sunday"); break; }

26 Elements of a loop Flowchart notation sum = 0 count = 0
initialization section false loop-repetition condition count < n Flowchart notation true enter a score sum = sum + score count = count + 1 repeat the loop loop body exit the loop loop body: the part that is repeated in the loop loop-repetition condition: decides when to repeat the loop and when to exit; the condition tests one or more variables (at least one modified in the loop body). initialization section: assigns initial values to the variables tested in the loop condition (such as count) and to other variables that are used before being given a value in the loop body (such as sum).

27 Loops: while statement
part of the loop not contained in the while stmt. Format while (condition) statement exit the loop condition true initialization statement false A while statement implements a pre-test loop which is repeated as long as the condition is true. The repetition ends when the condition is found to be false, and the execution continues with the statement that follows the loop body. The body of the loop must eventually cause the condition to become false, otherwise an infinite loop results. The loop body is a simple or a compound statement. a while loop may be executed zero times if the condition is found false when tested for the first time. repeat the loop Important: do not confuse the while statement (a loop) with the if statement (a selection which does not imply any repetition).

28 Accumulating a sum Trace of while loop int next; // the next number
int sum = 0; // the sum so far int count = 0; // initialize loop counter while (count < MAX) { // test loop counter readInt(next); // get next number sum = sum + next; // add next number to sum count = count + 1; // increment loop counter } // end loop Trace of while loop next sum count after initialization ? 0 0 sum and count set to 0, next undefined after 1st iter read into next and added to sum after 2nd iter read into next and added to sum after 3rd iter read into next and added to sum, . . .

29 Example ตัวอย่างการทดลองบวกเลข 1 ถึง 100
public class Series { public static void main (String[] args) { int x = 1; int total = 0; while (x <= 100) { total += x; x = x + 1; } System.out.println ("The series from 1 to 100 is " + total);

30 ข้อ2. ให้เขียนโปรแกรมชื่อ work1_2.java แก้ปัญหาดังนี้
ฝึกปฏิบัติ ข้อ2. ให้เขียนโปรแกรมชื่อ work1_2.java แก้ปัญหาดังนี้ หาผลรวมเลขจำนวนเต็มคู่ จากช่วงข้อมูลจำนวนเต็มที่ป้อนเข้าไป จากคีย์บอร์ด จากนั้นแสดง ผลรวมออกทางจอภาพ ตัวอย่างเช่น ช่วงข้อมูลที่ป้อนเข้าดังนี้ ข้อมูลแรก (Start Number = ป้อน 2) ข้อมูลสุด้าย(Stop Number = ป้อน 10) ดังนั้นผลรวมจำนวนเต็มคู่จึง = = 24 เป็นต้น (ทำหน้าเครื่องของตนเอง เสร็จแล้ว run พร้อมส่งเอกสารให้อาจารย์ในชั่วโมง)

31 for statement Format initialization of loop control variable
for (initialization; condition; update) statement exit the loop condition true initialization of loop control variable loop body false update loop control variable A for loop is a loop structure which is mainly used to implement counter-controlled loops a for loop may be executed zero times if the condition is false when first tested Example: two equivalent counter-controlled loops: one with for and the other with while for (int i = initial ; i <= final; i = i+step) { // insert loop body here } var i defined only inside the loop body int i = initial ; while (i <= final) { // insert loop body here i = i + step; }

32 for (ค่าตัวแปรเริ่มต้น ; เงื่อนไข ; เปลี่ยนแปลงค่าของตัวเลข )
{ คำสั่งต่าง ๆ ; } for ( count = 0 ; count <=10 ; count ++ ) { System.out.println("*");}

33 class For1 { public static void main(String[] args) { int i; int sum = 0; for (i=1;i<=10 ;i++ ) { sum+=i; System.out.println(" i = "+i+"sum = " + sum); } System.out.println("sum "+(i-1)+" = "+sum);

34 do-while syntax do { statement; } while(expression);
The Statement is executed, and then the boolean typed expression is evaluated. If it is false, execution drops through to the next statement. If it is true, you loop through the Statement again. This form of loop is for iterations that take place at least one time. If the Expression is false on the first evaluation, the Statement will already have execute once

35 public class Testdo { public static void main (String args[])
{int data =1; int sum = 0; do { sum += data ; data = data+1; System.out.println ("data = " +data+" Sum : " + sum) ; } while (data <=10) ; System.out.println("========================="); System.out.println ("data = " +(data-1)+" Sum : " + sum) ; } } // Testdo

36 ข้อ3. ให้เขียนโปรแกรมชื่อ work1_3.java แก้ปัญหาดังนี้
ฝึกปฏิบัติ ข้อ3. ให้เขียนโปรแกรมชื่อ work1_3.java แก้ปัญหาดังนี้ ให้เขียนโปรแกรมแสดงสูตรคูณตั้งแต่ สูตรคูณแม่ 1 ถึง 12 ออกทางจอภาพ (ให้ใช้ for loop) (ทำหน้าเครื่องของตนเอง เสร็จแล้ว run พร้อมส่งเอกสารให้อาจารย์ในชั่วโมง)

37 ข้อ4. ให้เขียนโปรแกรมชื่อ work1_4.java
ฝึกปฏิบัติ ข้อ4. ให้เขียนโปรแกรมชื่อ work1_4.java ให้เขียนโปรแกรมรับค่าเงินเดือนพนักงาน(Salary) โดยถ้าเงินเดือนตั้งแต่ ขึ้นไปเสียภาษี(TAX) 5 % จากเงินเดือนนอกนั้นไม่เสีย จากนั้นคิดเงินสุทธิที่ได้รับ(NET = SALARY-TAX) แล้ว แสดง เงินสุทธิ(NET) รวมของพนักงานทั้งหมด ออกทางจอภาพทั้งนี้บริษัทนี้มีพนักงาน 50 คน (ไม่ต้องแสดงรายละเอียดแต่ละคน แต่ให้คำนวณเงินสุทธิรวมเพียงอย่างเดียว แล้วแสดงผลการคำนวณออกทางจอภาพ)

38 ต่อไป การประมวลผล Arrays and string


ดาวน์โหลด ppt ครั้งที่ 3.

งานนำเสนอที่คล้ายกัน


Ads by Google