Back to 课程

Computer-Science-A-level-Ocr

0% Complete
0/0 Steps
  1. 3-3-networks
    8 主题
  2. 3-2-databases
    7 主题
  3. 3-1-compression-encryption-and-hashing
    4 主题
  4. 2-5-object-oriented-languages
    7 主题
  5. 2-4-types-of-programming-language
    4 主题
  6. 2-3-software-development
    5 主题
  7. 2-2-applications-generation
    6 主题
  8. 2-1-systems-software
    8 主题
  9. 1-3-input-output-and-storage
    2 主题
  10. 1-2-types-of-processor
    3 主题
  11. 1-1-structure-and-function-of-the-processor
    1 主题
  12. structuring-your-responses
    3 主题
  13. the-exam-papers
    2 主题
  14. 8-2-algorithms-for-the-main-data-structures
    4 主题
  15. 8-1-algorithms
    10 主题
  16. 7-2-computational-methods
    11 主题
  17. 7-1-programming-techniques
    14 主题
  18. 6-5-thinking-concurrently
    2 主题
  19. 6-4-thinking-logically
    2 主题
  20. 6-3-thinking-procedurally
    3 主题
  21. 6-2-thinking-ahead
    1 主题
  22. 6-1-thinking-abstractly
    3 主题
  23. 5-2-moral-and-ethical-issues
    9 主题
  24. 5-1-computing-related-legislation
    4 主题
  25. 4-3-boolean-algebra
    5 主题
  26. 4-2-data-structures
    10 主题
  27. 4-1-data-types
    9 主题
  28. 3-4-web-technologies
    16 主题
课 Progress
0% Complete

Iteration

What is iteration?

  • Iteration is the process of doing something more than once (repeat), otherwise known as a loop

  • A loop can be count controlled which means the code is repeated a fixed number of times

  • A loop can also be condition controlled which means the code is repeated until a condition is met

  • Three common loops are:

    • for loops (count controlled)

    • while loops (condition controlled)

    • do while loops (condition controlled)

For Loops

  • A for loop is a count controlled loop that will repeat a fixed number of times. It provides a concise and structured way to perform repetitive tasks.

Syntax of a for loop

The syntax of a for loop consists of three main parts:

01 for i = x to y
02 // Code to be executed in each iteration
03 next i

  1. Initialisation: The initialisation is executed only once at the beginning of the loop. It is used to initialise a counter variable that controls the loop’s execution which is i in this example

  2. Range: The range that the count variable will increment through

  3. Increment/Decrement: The default is to increment by 1 each time unless specified

Pseudocode example

xp66-srn-for-loop-pseudocode-computer-science-revision-notes

for loop example pseudocode

Python example

01 for i in range(0,6):
02 print i

In Python, the range specifies the numbers used in the loop. The final number (6 in this case) is 1 higher than the number we want to run the loop with.

Java example

01 for (int i = 0; i < 6; i++) {
02 System.out.println(i);
03 }

Iterating over an array

Pseudocode example

for-loop-list-array-pseudocode-computer-science-revision-notes

for loop iterating over an array and outputting each item

Python example

01 fruits = ["apple", "banana", "orange"]
02 for i in range(len(fruits)):
03 print(fruits[i])

Java example

01 String[] fruits = {"apple", "banana", "orange"};
02 for (int i = 0; i < fruits.length; i++) {
03 System.out.println(fruits[i]);
04 }

While Loops

  • A while loop is a condition controlled loop that will repeat until a condition is met 

  • While loops provide a flexible and powerful way to handle situations where the number of iterations is unknown in advance

Syntax of a while loop

  • The syntax of a while loop consists of a condition and a code block:

01 while condition
02 // Code to be executed as long as the condition is true
03 endwhile

  • The condition is evaluated before each iteration. If the condition evaluates to true, the code block is executed

  • If the condition evaluates to false, the loop terminates.

Pseudocode example

while-loop-pseudocode-computer-science-revision-notes

while loop example pseudocode

Python example

01 i = 0
02 while i <= 5:
03 print(i)
04 i = i + 1

Java example

01 int i = 0;
02 while (i <= 5) {
03 System.out.println(i);
04 i = i + 1;
05 }

Checking if the password is ‘secret’

Pseudocode example

while-loop-password-pseudocode-computer-science-revision-notes

while loop checking if the password is correct

Python example

01 password = ""
02 while password != "secret":
03 password = input("What is the password? ")

Java example

01 import java.util.Scanner;
02 public class Main {
03 public static void main(String[] args) {
04 Scanner scanner = new Scanner(System.in);
05 String password = "";
06 while (!password.equals("secret")) {
07 System.out.print("What is the password? ");
08 password = scanner.nextLine();
09 }
10 scanner.close();
11 }
12 }

Examiner Tips and Tricks

  • Incrementing a variable can be done in different ways (depending on the language)

  • For example:

    • i= i + 1

    • i + = 1

    • i++

Worked Example

A simple Python program is shown below.
01 //Program to calculate the number of times a number goes into 100
02
03 count = 0
04 num = int(input("Enter a number"))
05 while (count*num)<=100
06 count=count+1
07 endwhile
08 count=count-1 //Take one off as gone over
09 print(str(num) + " goes into 100 " + str(count) + " times.")

State the output of the program when the number 30 is entered.

[1]

How to answer this question:

  • If 30 is entered this is saved in the variable num

  • The while loop will run while count*num is less than or equal to 100

  • count is 0, so 30 * 0 = 0

  • As there is a loop to iterate through it would be useful to produce a trace table to help us keep track of the value of the different variables

count

num

count * num

0

30

 

1

 

30

2

 

60

3

 

90

4

 

120

3

 

 

  • The loop will repeat and when count is 4, count*num is 120 which causes the condition to be false and the loop to stop

  • Count is decremented

  • The statement which is printed is 30 goes into 100 3 times

Answer:

30 goes into 100 3 times.

Do While Loops

  • A do while loop is another example of a condition controlled loop that will repeat until a condition is met. 

  • Do while loops provide a variation of the while loop with a slightly different behaviour

  • The code within a do while loop will always run at least once, unlike a while loop which may not run at all if the condition is already met

Syntax of a do while loop

  • The syntax of a do while loop consists of a code block and a condition:

01 do
02 

Responses

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