Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.

Similar presentations


Presentation on theme: "ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that."— Presentation transcript:

1 ITERATION

2 Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that computers do well and people do poorly. Repeated execution of a set of statements is call iteration.

3 ASSIGNMENT It is legal to make more than one assignment to the same variable

4 Updating variables One most common forms of assignment is an update, where the new value of the variable depend on its old value.

5 Updating variables If try to get the value of variable that has never been assigned to, you will get an error.

6 The for loop revisited

7

8

9 The while statement 1.Evaluate the condition at line 5 2.If the value is False, exit the while statement 3. If the value is True, execute each of the statement in the body and then go back to the while statement at line 4

10 The while statement

11 The for statement

12 The print function has an extra argument end=“, “ This tells the print function to follow the printed string with whatever the programmer chooses (in this case, a comma followed by a space), in stead of ending the line. So each time something is printed in the loop, it is printed on the same output line, with the numbers separated by commas.

13 Print the value of n followed by a period and a newline character.

14

15 Counting digits

16 Tracing a program To write effective programs, and to build a good conceptual model of program execution, a programmer needs to develop the ability to trace the execution of a program.

17 Abbreviated assignment

18 Help and meta-notation Python come with extensive documentation for all its built-in function, and its library. Different systems have different ways of accessing this help. In PyScripter, click on the help menu item, and select Python manual.

19

20

21

22 Tables

23 Counting either O or 5 digits

24 Two-dimentional tables A two-dimentional table is a table where you can read the value at the intersection of a row and a column

25 A good way to start Write a loop that prints the multiples of 2

26 Encapsulation Encapsulation is the process of wrapping a piece of code in a function. To divide a large complex program into components (like functions) and isolate the components from each other (by using local variables, for example).

27 Generalization Generalization means taking something specific, such as printing the multiples of 2, and making it more general, such as printing the multiples of any integer. To replace something unnecessarily specific (like a constant value) with something appropriately general (like a variable or parameter). Generalization makes code more versatile, more likely to be reused, and sometimes even easier to write.

28 This function encapsulate the loop and generalizes it to print multiples of n To encapsulate was add the first line, which declares the name of the function and the parameter list. To generalize was replace the value 2 with the parameter n.

29 If this function was called with the argument 2,3

30 By now you can guess how to print a multiplication table By calling print_multiples repeatly with different arguments

31 How to print a multiplication table In fact we can use another loop

32 More encapsulation To demonstrate encapsulation, let’s wrap the code up in a function.

33 Local variables How we can use the same variable, i, in both print_multiple and print_multiples. The answer is the i in print_multiples(n) and the i in print_multiple() are not the same variable.

34 Local variables Variables created inside a function definition are local, you can’t access a local variable from outside its home function. That means you are free to have multiple variables with the same name as long as they are not in the same function.

35 n  3, i  1 2 i  1 2 3 print_multiple print_multiples

36 The break statement The break statement is used to immediately leave the body of its loop. The next statement to be executed is the first one after the body

37 The pre-test loop : standard loop behaviour for and while loops do their tests at the start, before executing any part of the body.

38 The middle-test loop Sometimes we’d like to have the middle-test loop with the exit test in the middle of the body.

39 The middle-test loop

40 The middle test loop

41

42

43

44

45 The continue statement The program immediately skip the processing of the rest of the body of the loop, for the current iteration. But the loop still carries on running for its remaining iterations:

46

47 More generalization A multiplication table of any size

48 More generalization If we want the table to be square - With the same number of rows and columns. To do that we add another parameter to print_multiples to specify how many columns the table should have.

49 More generalization Print only half the table

50 Function แบ่งงานให้เป็นงานย่อยหลายๆงาน และตั้งชื่อ งานตามหน้าที่ แบ่งโปรแกรมยาวเป็นหลายฟังก์ชัน ทดสอบ และแก้ไขแต่ละฟังก์ชัน ฟังก์ชันที่ออกแบบดี เมื่อทดสอบใช้งานได้ดี แล้ว จะสามารถนำกลับมาใช้งานได้อีก

51 Paired Data

52

53 Nested loops for nested data

54 Nested loop for nested data

55 Newton’s method for finding square roots


Download ppt "ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that."

Similar presentations


Ads by Google