Introduction to
Programming
Computer Science
Lesson 4
Unit 8.2
Objectives
• Understand what Iteration means
• Realise the importance of Iteration
• Understand and write While Loops & Do While Loops
What is a Iteration?
Iteration means to repeat something again and again and again
……..
Sometimes you might want to repeat a section of your program:
- a fixed number of times
- until something happens
- while a particular condition exists
- forever!
In programming, these are called loops
Why is Iteration important?
Iteration or Loops are important as they allow you repeat a bit of
Code as many times as you like, for instance;
- Print the word “Hello” a Million times
- Count the score in a computer game
- Perform repeated complex calculations on a NASA Rocket
There are 3 types of Loops, we will start by looking at the first two:
- While Loops
- Do While Loops
- For Loops
While Loops
While Loops are controlled by a condition, the condition decides how many time it will loop!
Teacher jumps = 0
While (Teacher jumps < 5) The condition is in red and bold.
The number of times the teacher has jumped = 0
{
Show video
While Loops
While Loops are controlled by a condition, the condition decides how many time it will loop!
Teacher jumps = 0
While (Teacher jumps < 5) The condition is in red and bold.
When will the teacher stop jumping?
{
Teacher jumps
Teacher jumps = Teacher jump +1 What is this instruction doing?
}
While Loops Example 1
While Loops are controlled by a condition, the condition decides how many time it will loop!
Teacher jumps = 0
While (Teacher jumps > 5) What would happen in this Loop?
How many times will the teacher Jump?
{
Teacher jumps
Teacher jumps = Teacher jump +1
}
While Loops Example 2
While Loops are controlled by a condition, the condition decides how many time it will loop!
Laps = 0
What is happing in this While loop?
Score = 0
- What is the condition, when will the
While ( Laps < 50)
race stop?
{
- What happens to the Score on each
Continue Racing loop?
Score = Score +1 - What happens to the Laps on each
Laps = Laps +1 loop?
}
Do While Loops
Do While Loops are controlled by a condition, the condition decides how many time it will loop!
Teacher jumps = 0
Do Do While Loops are exactly the
same as while loops except the
{ condition is at the end. What
difference does that make?
Teacher jumps
Teacher jumps = Teacher jump +1
}
The condition has moved here!
While (Teacher jumps < 5)
Reminder of Operators
Operators also allow
you to compare data
in programming,
which will also let
your program make
decisions.
Exercise 1
Open Worksheet 1 on Firefly and complete Task 1
What did we learn today?
What are iterations
Understand the purpose of loops/iterations
Understand the structure of a While loop & Do while loops
Create while loops and Do while loops with different conditions