0% found this document useful (0 votes)
14 views

Looping-Through-Algorithms--Understanding-Repetition-in-Programming

notes on looping through algorithm year 9 computing
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Looping-Through-Algorithms--Understanding-Repetition-in-Programming

notes on looping through algorithm year 9 computing
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

LOOPING THROUGH ALGORITHMS:

UNDERSTANDING REPETITION IN PROGRAMMING


LEARNING OBJECTIVES

• Understand the purpose and importance of loops in algorithms


• Identify and explain different types of loops (FOR, WHILE, DO-
WHILE)
• Combine loops with conditional statements effectively
• Create and interpret pseudocode representing algorithms with
loops
• Develop collaborative skills through pair programming
exercises
WHY DO WE NEED LOOPS?

• Loops allow repetition of code without writing it multiple times


• They make programs more efficient and easier to read
• Loops are essential for tasks like:
• Processing lists of data
• Repeating actions until a condition is met
• Creating game loops or animations
TYPES OF LOOPS: FOR LOOP

• Used when we know how many times to repeat


• Structure:
FOR (initialization; condition; update)

// code to repeat

END FOR
• Example: Counting from 1 to 5
FOR count = 1 TO 5

PRINT count

END FOR
TYPES OF LOOPS: WHILE LOOP

• Repeats as long as a condition is


true IF guess < correct_number THEN
• Structure:
WHILE (condition) PRINT "Too low!"

// code to repeat ELSE IF guess > correct_number THEN

END WHILE PRINT "Too high!"


• Example: Guessing a number
SET correct_number = 42 END IF

WHILE guess != correct_number END WHILE

INPUT guess PRINT "Correct!"


TYPES OF LOOPS: DO-WHILE
LOOP
• Similar to WHILE, but always PRINT "3. Quit"
executes at least once
• Structure: INPUT choice
DO
IF choice = 1 THEN
// code to repeat
CALL start_game()
WHILE (condition)
• Example: Menu selection ELSE IF choice = 2 THEN
DO
CALL open_settings()
PRINT "1. Start Game"
END IF
PRINT "2. Settings"
WHILE choice != 3
COMBINING LOOPS WITH CONDITIONAL
STATEMENTS
• IF, ELSE, THEN can be used inside loops
• Example: Checking even numbers in a loop
FOR number = 1 TO 10

IF number MOD 2 = 0 THEN

PRINT number + " is even"

ELSE

PRINT number + " is odd"

END IF

END FOR
USING PSEUDOCODE FOR LOOPS
• Pseudocode is a simple way to plan algorithms
• Example: Finding the largest number in a list
SET largest = first number in list

FOR each number in the list

IF number > largest THEN

SET largest = number

END IF

END FOR

PRINT largest
COLLABORATIVE SKILLS: PAIR PROGRAMMING

• Work in pairs to solve loop-based problems


• Driver: Types the code and explains their thinking
• Navigator: Reviews the code, suggests improvements
• Switch roles every 10-15 minutes
• Benefits: Better code quality, shared learning, improved
communication
PRACTICE EXERCISE

In pairs, create a pseudocode algorithm that:


• Asks the user for a positive number
• Uses a loop to print all even numbers up to that
number
• If the number is negative, asks again until a
positive number is entered
Hint: Combine WHILE, FOR, and IF statements
• We've learned about:
REVIEW AND QUESTIONS
• The importance of loops in programming
• FOR, WHILE, and DO-WHILE loops
• Combining loops with conditionals
• Using pseudocode for algorithms with loops
• Collaborative programming skills
• Any questions about loops or pseudocode?

You might also like