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

Slides:



Advertisements
Similar presentations
Introduction to Computing Science and Programming I
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Chapter 5: Control Structures II (Repetition)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Adding Automated Functionality to Office Applications.
Introduction to Computer Programming in c
Fundamentals of Python: From First Programs Through Data Structures
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Fundamentals of Python: First Programs
Chapter 2 Control. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Repetition, quick overview.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
CPS120 Introduction to Computer Science Iteration (Looping)
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 05 (Part III) Control Statements: Part II.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
JavaScript, Fourth Edition
CPS120 Introduction to Computer Science Iteration (Looping)
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Nested Loops CS303E: Elements of Computers and Programming.
Chapter 4 – C Program Control
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Topics Introduction to Repetition Structures
CS 115 Lecture 8 Structured Programming; for loops
Chapter 2.2 Control Structures (Iteration)
Topics Introduction to Repetition Structures
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Additional Control Structures
The University of Texas – Pan American
Chapter 2.2 Control Structures (Iteration)
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
2.6 The if/else Selection Structure
For loops Taken from notes by Dr. Neil Moore
Flow of Control.
Chapter 4 - Program Control
Loops CGS3416 Spring 2019 Lecture 7.
Class code for pythonroom.com cchsp2cs
Looping and Repetition
Presentation transcript:

ITERATION

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.

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

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

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

The for loop revisited

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

The while statement

The for statement

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.

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

Counting digits

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.

Abbreviated assignment

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.

Tables

Counting either O or 5 digits

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

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

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).

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.

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.

If this function was called with the argument 2,3

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

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

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

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.

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.

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

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

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

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

The middle-test loop

The middle test loop

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:

More generalization A multiplication table of any size

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.

More generalization Print only half the table

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

Paired Data

Nested loops for nested data

Nested loop for nested data

Newton’s method for finding square roots