 # Fixed loops

## What is the output of the following program?

``````fortnight_players = 784
wow_players = 813

if fortnight_players > 700 and wow_players > 700:
if fortnight_players > wow_players:
print("We have more Fortnight players")
else:
print("We have more WoW players")``````

## What is the output of the following program?

``````fortnight_players = 784
wow_players = 813

if fortnight_players > 700 and wow_players > 700:
if fortnight_players > wow_players:
print("We have more Fortnight players")
else:
print("We have more WoW players")``````

We have more WoW players

## Learning Intentions

• Understand what a loop is in computer science
• Implement a loop in Python

## Success Criteria

• I can explain the benefit of loops in computer programming
• I am able to develop a fixed loop in Python

## A loop?

In pairs, discuss for three minutes what you think a loop would do in a computer programming language.

## What a loop does

• A loop will do something over and over until a termination case has been met. This basically means when the computer is told that it is time to stop.
• Some loops, called infinite loops, will never terminate. An example of this is one that will keep a slideshow on a website running forever.
• The body of the loop is the code that is repeated.
• There are two kinds of loops: fixed loops and conditional loops

• Write once, do many times. Just write the code to loop and the body once rather than copying and pasting over and over.
• The number of times a loop is needed might need to change often (even whilst the program runs). If you did this manually you would need to copy and paste or delete code over and over (example in next slide)

## Without loops

``````n = 0

print(n)
n = n + 1

print(n)
n = n + 1

print(n)
n = n + 1

print(n)
n = n + 1``````

## With loops

``````n = 0

for i in range(0, 4):
print(n)
n = n + 1``````

## With loops - simplified

``````for i in range(0, 4):
print(i)

``````

i is a local variable that changes each time the loop repeats

## Fixed loops

• A fixed loop will loop a set number of times.
• We give a fixed loop a range to loop between a lower number (e.g. 0) and a larger number (4) that tells the computer how many times to loop. Range is a Python predefined function, so its arguments go between the brackets and are separated by a comma:
• range(0, 4)
• We can figure out how many times it will loop by taking the smaller number from the bigger number:
• range(0, 4) : 4 – 0 = 4
• range(10, 5) : 10 - 5 = 5

## How many times will this loop?

``````for i in range(0, 5):
print(i)

``````

## How many times will this loop?

``````for i in range(9, 12):
print(i)

``````

## How many times will this loop?

``````for i in range(15, 30):
print(i)

``````

## Loops inside loops

• Just like with if statements you can have nested loops.
``````for num1 in range(0, 5):
for num2 in range(0, 5):
print(num1, num2)

``````

This will loop 25 times (5 x 5)

## Loops inside loops

• Just like with if statements you can have nested loops.
``````for num1 in range(0, 5):
for num2 in range(0, 5):
print(num1, num2)

``````

This will loop 25 times (5 x 5)

## How many times will this loop?

``````for num1 in range(5, 10):
for num2 in range(0, 10):
print(num1, num2)

``````

## Getting the remainder

• When performing a division of two numbers, e.g. x and y, the number you get back from this is called the quotient.
• Thinking about a group of five people. If half of them go on holiday, how many of them went on holiday?
• 2 or 3
• Or would it be 2.5 people?
• We cannot have a half person, so instead we have 2 remainder 1.
• Visit the website DIVISIONANDREMAINDERS and read the information on there.

Calculate the quotient and remainder for each of the following:

7 ÷ 3

9 ÷ 7

81 ÷ 21

## Remainder in Python

``````x = 9 / 4
y = 8 % 3``````

## Even and odd numbers

• Any number when divided by 2 which gives a remainder of 0 is an even number.
• Any number when divided by 2 which gives a remainder of 1 is an odd number.

• To use this, we can write an if statement to check this:
``````number = 7

if number % 2 == 0:
print("This is an even number")``````
1. Write a for loop that will display each number in the six times table (6, 12, 18...).

1. Complete the Python Fixed Loops (For) tasks in the Python book on page 29

Presentation Overview
S3 Computer Science :: Software Development :: Fixed loops
21:08 | 30-09-2023
!
Keywords
Sections
Binary conversion
Denary to binary conversion
Binary to denary conversion
Slideshow Outline
Feedback 👍
Accessibility

Apply a filter:

×
All slide files