Iteration in Computer Science: WHILE, REPEAT, and FOR Loops, Slides of Computer science

This resource provides a comprehensive overview of iteration in programming, focusing on while, repeat, and for loops. It explains syntax, usage, range checks, and how to avoid infinite loops. Random number generation and nested for loops are also covered, with practical examples and pseudocode. Designed for high school students, it aims to enhance problem-solving skills and code efficiency by building a solid foundation in iterative programming. Worksheets and tasks encourage active learning, making it ideal for classroom instruction and self-study.

Typology: Slides

2024/2025

Uploaded on 07/20/2025

politics-now
politics-now 🇬🇧

5 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
3
AQA
AS Level
Computer
Science
Paper 1
Iteration
Unit 1
Fundamentals
of programming
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Iteration in Computer Science: WHILE, REPEAT, and FOR Loops and more Slides Computer science in PDF only on Docsity!

AQA

AS Level

Computer

Science

Paper 1

Unit 1 Fundamentals of programming

Objectives

  • (^) Understand and use three different types of iterative

statement WHILE, REPEAT and FOR

  • (^) Be familiar with, and be able to use, random number

generation

Unit 1 Fundamentals of programming WHILE … ENDWHILE loop

  • (^) Using a WHILE .. ENDWHILE loop, the condition

is tested upon entry to the loop

  • (^) It is possible that the instructions inside the loop

might not be executed at all if the entry condition

is not met

WHILE Condition x = True Execute statement a Execute statement b etc.... END WHILE

Unit 1 Fundamentals of programming Iteration entry condition

  • (^) Trace through the pseudocode loop
    • (^) What values will x take?
    • (^) What will be the output?
  • (^) What would happen if the first statement was

changed to x  2?

x  0 WHILE x < 2 x  x+ OUTPUT x ENDWHILE OUTPUT “The end” x Output 0

Unit 1 Fundamentals of programming Worksheet 3

  • (^) Complete Questions 1 and 2 in Task 1

Unit 1 Fundamentals of programming REPEAT .. UNTIL loop

  • (^) In a REPEAT. .UNTIL loop the statements in the

loop are executed before the condition is evaluated

  • (^) The statement will always be executed at least once
  • (^) Complete the values for x and Output
  • (^) What would happen if the first statement was

changed to x  2?

x  0 Repeat x x+ OUTPUT x UNTIL x >= 2 OUTPUT “The end” x Output 0

Unit 1 Fundamentals of programming Performing a range check

  • (^) A loop can be used to implement a range check
  • (^) The loop will continually prompt for input until a valid

age between 12 and 18 is entered

  • (^) Work through the algorithm below with the data 10,

11,18 and 19. What will be the output?

REPEAT

OUTPUT “Enter age” age USERINPUT UNTIL age > 11 AND age <= 18 OUTPUT “Age is”, age

Unit 1 Fundamentals of programming An equivalent WHILE loop

  • (^) In Python the REPEAT..UNTIL loop is not supported
  • (^) An equivalent loop can be created using a WHILE

loop

  • (^) Work through the algorithm below with the data 10,

11,18 and 19. What will be the output?

age = 0 WHILE age < 12 OR age > 18 OUTPUT “Enter age” age USERINPUT ENDWHILE OUTPUT “Age is”, age

Unit 1 Fundamentals of programming Infinite loop

  • (^) Infinite loops are often used in 2D games
    • (^) An “outer” WHILE True… ENDWHILE loop runs the game
    • (^) A brief pause can be included to slow the loop down, so that a sprite x,y position can be updated about 25 times a second for smooth animation WHILE True updatePlayerPosition() checkForCollision() redraw() ENDWHILE

Unit 1 Fundamentals of programming Infinite loops in control and data sensing applications

  • (^) Computer control and data sensing applications use

infinite loops to gather data from sensors

  • (^) A variety of sensors can control a number of output devices such as lights, buzzers and motors
  • (^) After the setup code is run, the device enters an infinite loop to repeatedly check the value of the sensors

Unit 1 Fundamentals of programming FOR .. NEXT loop

  • (^) The FOR .. NEXT loop is termed “definite iteration”,

and is used to repeat a block of instructions a

specified number of times

  • (^) The FOR loop uses a counter variable which is

automatically incremented each time through the

loop

  • (^) Optionally, a STEP value can be specified to make

the counter increase or decrease by any integer

Unit 1 Fundamentals of programming FOR .. NEXT loop

  • (^) What values will index and x take? FOR index  1 TO 8 x = (index ** 2) % 3 OUTPUT x NEXT index index x 1

Unit 1 Fundamentals of programming Stepping backwards

  • (^) The increment in a FOR loop can be negative
  • (^) What values will index and x take? FOR index  15 to 1 Step
    • x = int (index /2) OUTPUT x NEXT index index x 15

Unit 1 Fundamentals of programming Using a WHILE .. ENDWHILE loop as an alternative

  • (^) Are these loops logically equivalent?
  • (^) What number would you use in the FOR loop to get

the same results as the WHILE loop?

  • (^) Outside the FOR loop, the variable index is

undefined

index  1 WHILE index < 4 OUTPUT index index  index + 1 ENDWHILE FOR index  1 to? OUTPUT index NEXT index