13
CS100: Computer Science Practice and Design Studio © James Moscola David Babcock / Don Hake Department of Physical Sciences York College of Pennsylvania CS100: CPADS Iteration (Loops)

CS100: CPADS - GitHub Pages · 2018. 10. 15. · CS100: Computer Science Practice and Design Studio Iteration • What is iteration? - Repeatedly executing a sequence of statements

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS100: CPADS - GitHub Pages · 2018. 10. 15. · CS100: Computer Science Practice and Design Studio Iteration • What is iteration? - Repeatedly executing a sequence of statements

CS100: Computer Science Practice and Design Studio © James Moscola

College Catalog2009–2011

!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633

05?3352775?30?EEEF+C:F(A;

!""#$%%&'$#()*$&+$,-$%.$"

'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK

!<GR%&'@'HGPOJ&N<F&012

YO

RK

CO

LLEGE O

F PENN

SY

LVA

NIA

CO

LLEGE C

ATA

LOG

2009–2011

!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56

College Catalog2009–2011

!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633

05?3352775?30?EEEF+C:F(A;

!""#$%%&'$#()*$&+$,-$%.$"

'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK

!<GR%&'@'HGPOJ&N<F&012

YO

RK

CO

LLEGE O

F PENN

SY

LVA

NIA

CO

LLEGE C

ATA

LOG

2009–2011

!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56

College Catalog2009–2011

!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633

05?3352775?30?EEEF+C:F(A;

!""#$%%&'$#()*$&+$,-$%.$"

'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK

!<GR%&'@'HGPOJ&N<F&012

YO

RK

CO

LLEGE O

F PENN

SY

LVA

NIA

CO

LLEGE C

ATALO

G 2009–2011

!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56

David Babcock / Don HakeDepartment of Physical SciencesYork College of Pennsylvania

CS100: CPADSIteration (Loops)

Page 2: CS100: CPADS - GitHub Pages · 2018. 10. 15. · CS100: Computer Science Practice and Design Studio Iteration • What is iteration? - Repeatedly executing a sequence of statements

CS100: Computer Science Practice and Design Studio

Iteration

• What is iteration? - Repeatedly executing a sequence of statements

• May need to do the same thing over-and-over again- Also referred to as looping

• Use fixed iteration when the number of iterations is known at the time of programming - The iterations are tracked by a loop counter

• Use conditional iteration when the number of iterations is based on a logical condition that is updated inside the loop (will be covered another day)

2

Page 3: CS100: CPADS - GitHub Pages · 2018. 10. 15. · CS100: Computer Science Practice and Design Studio Iteration • What is iteration? - Repeatedly executing a sequence of statements

CS100: Computer Science Practice and Design Studio

Structure of a for-loop

• Most programming languages (including Python) use the keyword ‘for’ to indicate the beginning of a ‘loop’

3

[code before the loop] # executes once

for loop_counter in range(num_loops): [indented Python statements] # executes num_loops times [include the work that you want to repeat] # executes num_loops times

[the code after the loop is not indented] # executes once

Page 4: CS100: CPADS - GitHub Pages · 2018. 10. 15. · CS100: Computer Science Practice and Design Studio Iteration • What is iteration? - Repeatedly executing a sequence of statements

CS100: Computer Science Practice and Design Studio

Structure of a for-loop

• Most programming languages (including Python) use the keyword ‘for’ to indicate the beginning of a ‘for-loop’

4

[code before the loop] # executes once

for loop_counter in range(num_loops): [indented Python statements] # executes num_loops times [include the work that you want to repeat] # executes num_loops times

[the code after the loop is not indented] # executes once

Each for-loop starts with the Python keyword for

Page 5: CS100: CPADS - GitHub Pages · 2018. 10. 15. · CS100: Computer Science Practice and Design Studio Iteration • What is iteration? - Repeatedly executing a sequence of statements

CS100: Computer Science Practice and Design Studio

Structure of a for-loop

• Most programming languages (including Python) use the keyword ‘for’ to indicate the beginning of a ‘for-loop’

5

[code before the loop] # executes once

for loop_counter in range(num_loops): [indented Python statements] # executes num_loops times [include the work that you want to repeat] # executes num_loops times

[the code after the loop is not indented] # executes once

Each for-loop contains a loop_counter variable that starts at 0 and increases after

each pass through the loopNote that the loop_counter variable can be named anything you like, just like any other variable. Additionally,

the loop_counter is AUTOMATICALLY assigned 0 when the loop first runs.

Page 6: CS100: CPADS - GitHub Pages · 2018. 10. 15. · CS100: Computer Science Practice and Design Studio Iteration • What is iteration? - Repeatedly executing a sequence of statements

CS100: Computer Science Practice and Design Studio

Structure of a for-loop

• Most programming languages (including Python) use the keyword ‘for’ to indicate the beginning of a ‘for-loop’

6

[code before the loop] # executes once

for loop_counter in range(num_loops): [indented Python statements] # executes num_loops times [include the work that you want to repeat] # executes num_loops times

[the code after the loop is not indented] # executes once

The in range keywords are part of the for-loop declaration and should be used

in conjunction with the for keyword

Page 7: CS100: CPADS - GitHub Pages · 2018. 10. 15. · CS100: Computer Science Practice and Design Studio Iteration • What is iteration? - Repeatedly executing a sequence of statements

CS100: Computer Science Practice and Design Studio

Structure of a for-loop

• Most programming languages (including Python) use the keyword ‘for’ to indicate the beginning of a ‘for-loop’

7

[code before the loop] # executes once

for loop_counter in range(num_loops): [indented Python statements] # executes num_loops times [include the work that you want to repeat] # executes num_loops times

[the code after the loop is not indented] # executes once

Specifies the number of times the loop will execute. Can be a literal value (e.g. 5, 22, etc.) or a variable

that is assigned before the for-loop starts.Note, the loop_counter variable will start at 0 and increase on each

pass through the loop until it equals num_loops-1.

Page 8: CS100: CPADS - GitHub Pages · 2018. 10. 15. · CS100: Computer Science Practice and Design Studio Iteration • What is iteration? - Repeatedly executing a sequence of statements

CS100: Computer Science Practice and Design Studio

How Many Numbers Do you See?

8

0 1 2 3 4What Is the Largest Number You See?

If num_loops=5, the loop_counter will range from 0 to 4

num_loops-1

Page 9: CS100: CPADS - GitHub Pages · 2018. 10. 15. · CS100: Computer Science Practice and Design Studio Iteration • What is iteration? - Repeatedly executing a sequence of statements

CS100: Computer Science Practice and Design Studio

Structure of a for-loop

• Most programming languages (including Python) use the keyword ‘for’ to indicate the beginning of a ‘for-loop’

9

[code before the loop] # executes once

for loop_counter in range(num_loops): [indented Python statements] # executes num_loops times [include the work that you want to repeat] # executes num_loops times

[the code after the loop is not indented] # executes once

The loop-body of the for-loop (i.e. the repeated code) contains one or more indented Python statements

Page 10: CS100: CPADS - GitHub Pages · 2018. 10. 15. · CS100: Computer Science Practice and Design Studio Iteration • What is iteration? - Repeatedly executing a sequence of statements

CS100: Computer Science Practice and Design Studio

A Simple Example

• Draw a square where each side is of length size

10

# Draw a square fd(t, size) rt(t, 90) fd(t, size) rt(t, 90) fd(t, size) rt(t, 90) fd(t, size) rt(t, 90)

1 2 3 4 5 6 7 8 9

Note that the code repeats itself.So, why write it over and over again?

Page 11: CS100: CPADS - GitHub Pages · 2018. 10. 15. · CS100: Computer Science Practice and Design Studio Iteration • What is iteration? - Repeatedly executing a sequence of statements

CS100: Computer Science Practice and Design Studio

A Simple Example

• Draw a square where each side is of length size

- Simplify with a for-loop

11

# Draw a square for i in range(4): fd(t, size) rt(t, 90)

1 2 3 4

This code will execute 4 times

Page 12: CS100: CPADS - GitHub Pages · 2018. 10. 15. · CS100: Computer Science Practice and Design Studio Iteration • What is iteration? - Repeatedly executing a sequence of statements

CS100: Computer Science Practice and Design Studio

A Simple Example

• Draw a square where each side is of length size

- Simplify with a for-loop

12

# Draw a square num = 4

for i in range(num): fd(t, size) rt(t, 90)

1 2 3 4 5 6

Remember, the range can also be specified using a variable

Page 13: CS100: CPADS - GitHub Pages · 2018. 10. 15. · CS100: Computer Science Practice and Design Studio Iteration • What is iteration? - Repeatedly executing a sequence of statements

CS100: Computer Science Practice and Design Studio

A More Interesting Example

• It is possible to use the loop_counter variable inside the loop

13

# Draw a shape size = 100 num = 4

for j in range(num): fd(t, size*j) rt(t, j*30)

1 2 3 4 5 6 7

j=0 fd(t, 0) rt(t, 0)

j=1 fd(t, 100) rt(t, 30)

j=2 fd(t, 200) rt(t, 60)

j=3 fd(t, 300) rt(t, 90)