Back to 课程

Computer Science GCES EDEXCEL

0% Complete
0/0 Steps
  1. Decomposition And Abstraction Edexcel
    2 主题
  2. Algorithms Edexcel
    11 主题
  3. Truth Tables Edexcel
    3 主题
  4. Binary Edexcel
    6 主题
  5. Data Representation Edexcel
    4 主题
  6. Data Storage And Compression Edexcel
    2 主题
  7. Hardware Edexcel
    5 主题
  8. Software Edexcel
    3 主题
  9. Programming Languages Edexcel
    2 主题
  10. Networks Edexcel
    7 主题
  11. Network Security Edexcel
    2 主题
  12. Environmental Issues Edexcel
    1 主题
  13. Ethical And Legal Issues Edexcel
    3 主题
  14. Cybersecurity Edexcel
    2 主题
  15. Develop Code Edexcel
    6 主题
  16. Constructs Edexcel
    4 主题
  17. Data Types And Data Structures Edexcel
    5 主题
  18. Operators Edexcel
    1 主题
  19. Subprograms Edexcel
    2 主题
课 Progress
0% Complete

Exam code:1CP2

Repetition & Iteration

What is the difference between repetition and iteration?

  • Repetition is executing the same instructions whilst a condition is True

  • Iteration is is executing the same instructions until all items in a data structure have been processed

  • Repetition & iteration are both examples of loops

Repetition

Python examples

num = 10

while num > 0:

print(num, "...")

num = num - 1

print("Blast off!")

The instructions inside the loop repeat until the num reaches 0

A higher starting number would result in more repeating

print("Do you accept the T&Cs? (Y/N)")

accept = input()

while accept != "Y":

print("Do you accept the T&Cs? (Y/N)")

accept = input()

print("Thank you for accepting the T&Cs")

Whilst the user does not input a “Y” to accept the terms & conditions the question is repeated infinitely

Iteration

Python examples

scores = [10, 5, 20, 20, 4, 5, 2, 25, 1]

for i in scores:

print(i * 2)

Outputs the values of the scores array, multiplied by 2 on separate lines

students =[

[1,"Bob",78],

[2,"Fred",24],

[3,"Julie",32]]

for student in students:

print ("Name:",

student[1],

"Test score:",

student[2])

The for loop processes each student in the students table.

In each loop the individual fields are outputted.

The output from this loop is:

Name: Bob Test score: 78

Name: Fred Test score: 24

Name: Julie Test score: 32

  • Repetition & iteration can be further confused with the addition of different types of loops

Type of loop

Description

Count-controlled

The number of loops is already known or can be determined before the loop starts

Condition-controlled

The number of loops is not known, it continues until the condition is True

Python example

Repetition or iteration?

Type of loop?

choice = "Y"

while (choice != "N"):

choice = input("choose: ")

Repetition

Condition-controlled

count = 0

while (count < 5):

count = count + 1

print (count)

Repetition

Count-controlled

table = ["cat", "dog", "fox"]

for word in table:

print (word)

Iteration

Count-controlled

  • In Python, when the range() function is used, the loop is count-controlled

  • If the loop is used to access a data structure it is iteration

Examiner Tips and Tricks

If the word ‘iteration‘ is used in an instruction, you can use a ‘for‘ loop to process a data structure.

If the word ‘repetition is used in an instruction, you must choose whichever construct (‘for‘, ‘while‘) is suitable for the program

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注