Bits of Bytes.co

IGCSE / GCSE Computer Science

  • Home
  • 1 - Theory of Computer Science
  • 2 - Practical Problem Solving and Programming
  • Pre-Release Material
    • 2018 June >
      • 22 >
        • 22 - Cow Task
        • 22 - Task 1
        • 22 - Task 2
        • 22 - Task 3
  • 1 - Computer Systems (2023)
Picture
MENU
  • Home
  • 1 - Theory of Computer Science
  • 2 - Practical Problem Solving and Programming
  • Pre-Release Material
    • 2018 June >
      • 22 >
        • 22 - Cow Task
        • 22 - Task 1
        • 22 - Task 2
        • 22 - Task 3
  • 1 - Computer Systems (2023)
2 - Practical Problem Solving & Programming / Iteration


Iteration


Iteration means to repeat.  The ability to have a program repeat steps without having to duplicate actual lines of code is vital for efficiency. 

Iteration

The term iteration means to repeat something over and over like a loop
Iteration can be used in coding to massively reduce the amount of code that needs to be written.

The two examples of code demonstrate just how much more efficient using a loop can be vs not using a loop.  Both code examples will output the numbers 1 to 12.  The code that uses no loop takes 12 lines whereas using a loop allows the code to work with only 4 lines.
Code With No Loop

    
Code With Loop

    
Picture
What is also interesting about this example is thate scale ability.  In reality 12 lines of code vs 4 is not going to make much of a difference. Now suppose that you needed to print the numbers 1 to 10 Million...

​The code with no loop would now need to be 10 million lines long but the code with a loop would still be only 4 lines long...
Code With Loop ( 1 to 10 Million)

    

Types of Iteration


There are 3 different types of iteration that we need to consider, these are:

For Loop
While loop
Repeat Until Loop

Each loop works in a very different way, has different abilities and is best suited to certain situations.
  • For Loops
  • While Loops
  • Repeat Until Loops
<
>

For Loops

A For loop is used when code needs to be repeated a specified number of times.  The amount of times that it will need to be repeated could be determined by the programmer, the program user or a calculation.

Example situations
  1. If you know that code is required to accept the names of exactly 30 competitions contestants then a For loop would be chosen.
  2. A program user will need to state how many products are available in their shop. Once the user has stated the number of items the program will then allow the user to enter the name of every item.  This code will need to repeat a set number of times, however the user is the person that sets how many times it will repeat.


Pseudocode Structure

CIE dictates that the Pseudocode for a FOR loop should use the following structure:
FOR Loop

    
<Identifier> =
This is a variable used for counting. Its value will start at (Value1) and increment by 1 after each repetition of the looping code. Once its value is equal to (Value2) the For loop will stop repeating.

(Value1) And (Value2) = 
These two values dictate how many times the loop will repeat.  The <identifier> variable will start at the value in place of (Value1) and it will repeat until the <identifier> reaches (Value2).
Both of these values can be integers set by the programmer OR they can be variables.  Making these values variables opens up the opportunity for the program user to dictate how many times a program will repeat.

<Statements> = 
Here is where you would put the code that needs to be repeated.

Example Situation - Basic For Loop

Each week Bob opens his shop for 4 days (Monday - Thursday). Bob needs a simple program that he can use to calculate how much money he has made at the end of his 4 day working week. Bob wants to be able to enter the total money made for each day and then be told the total money made for the week.

For this solution you know that the program will need to repeat 4 times. As you know that it has to repeat a set number of times a For loop is the most suitable
Picture

Example Situation 2 - Using Variables

 Bob's business has now changed in a way that makes the previous program unusable. Bob is now much more flexible in how many days a week his shop opens. Sometimes it may only open 2 times, sometimes it may open 7 times.  The current program will always ask him to enter the total money made for 4 days.

How can we fix the program?

The way to fix this program is to introduce a variable into the second value of the for loop (The upper range).  Before the loop begins we need to prompt Bob with the opportunity to enter the number of days that his shop has been open. Once Bob enters this amount of days it will then proceed to repeat that many times:
​
Picture

Example Situation 3 - Loops and Arrays

Lets say that you are given a situation where you need to ask the user to enter 10 peoples names.  You have decided that an Array would be the best way to store 10 names. The inefficient way of doing this would be to simply write repeated statements like below:
Array - Input 10 names with no loop

    
This could problem could actually be achieved far more efficiently by using a for loop.
We know that the code will need to repeat 10 times so a FOR loop is a good choice for this e.g. FOR x = 0 to 9

The counting variable (x) is the key player here.  As x will start at 0 and increase by 1 after each repetition we can use it to point to the array index that we wish to insert the name. See the example below:
Array - Input 10 names with no loop

    

Example Situation 4 - Loops and arrays 2

Lets take the previous example one step further. This time you will need the user to enter a number of names into an array, however the user will decide how many names need to be entered.

For this to work you will need to:
  1. Ask the user how many names there are and store it in a variable e.g. NoNames
  2. Declare the array and set the side using the "NoNames" variable
  3. Set the for loop to repeat using the "NoNames" variable as the upper limit.
Array - Input unknown number of names with loop

    

While Loops

While loops are also known as pre-condition loops.  This is because a while loop will repeat forever WHILE a set condition is true. This condition is checked at the start of the loop hence the name "pre-condition".

Example Uses
While loops are particularly useful when you do not know specifically how many times you want the code to repeat. e.g.​
  • A user should be able to use a program as many times as they like until they choose to quit
  • A dice game will allow the user to roll the dice until they choose to quit
  • A program should repeat forever (No way for the condition to be false)
  • Validating a user input e.g. not accepting a password if it is less than 8 characters long.

Pseudocode Structure

CIE dictates that the Pseudocode for a WHILE loop should use the following structure:
WHILE Loop

    
<Condition> =
The condition is the part that will be evaluated before executing the statements within the WHILE loop.  If the condition is true the statements will execute. If the condition is false then the statements will not be executed and the program will move past the WHILE  loop.
​
<Statements> = 
Here is where you would put the code that needs to be repeated.

Example Situation - Using While to validate input

When signing up for a new service it is quite common that the user will need to choose a new password. Often a condition is set that the password should be 8 characters  or greater for security reasons. If the user enters a password that is shorter than 8 characters then they should be not be permitted to continue until they change it to an acceptable one.

A common mistake here is to think that an IF statement will be enough to solve this problem e.g.
Validation using a IF

    
Why is this not a perfect solution?  Well imagine that the user first enters a password that is 5 characters long.  The IF statement would decide that this is less than 8 and then tell the user to enter a password greater than 8 characters. The user now has a chance to enter the password again, but this time they only enter a password that is 7 characters long. This password is still unacceptable, however, the code has no way of rejecting it and now an unacceptable password will be accepted.

To fix this problem we can introduce a WHILE loop.  A WHILE loop will keep repeating WHILE a condition is true. If you were to make the condition - Len(Password) < 8  The code will keep repeating until the length of the password is 8 or greater.
Validation using a WHILE loop

    
This code offers an improvement over the previous solution. Now the user will start by entering a password. Next the WHILE loop will check the length of the password. If the password length is less than 8, the user will be asked to enter the password again. This password will now be checked again, If it is still not 8 or more characters long then they will be asked to enter a new password. This process will repeat forever until the password meets the condition. Once the password has met the condition, the code will continue past the WHILE loop. 

Repeat Until Loops

A REPEAT UNTIL loop is also known as a Post-condition loop.  This type of loop will repeat forever UNTIL a condition becomes true.

Pseudocode Structure

CIE dictates that the Pseudocode for a REPEAT UNTIL loop should use the following structure:
REPEAT UNTIL Loop

    
<Condition> =
The condition is the part that will be evaluated before deciding whether the loop needs to repeat again.  The REPEAT UNTIL loop has its condition at the end of the loop. This means that the <Statements> are guaranteed to run at least once before the condition is checked.  This is the opposite of a WHILE loop where if the condition is false at the first time that the loop is attempted then the <statements> within the loop will never run.
​
<Statements> = 
Here is where you would put the code that needs to be repeated.
Vertical Divider
Est. 2015 - Copyright © 2020