Algorithms - EduqasCounts and rogue values

Algorithms are step-by-step plans for solving problems. They are a starting point when writing a program. Algorithms can be designed using pseudo-code and flowcharts.

Part ofComputer ScienceUnderstanding Computer Science

Counts and rogue values

When writing a program with a loop, the programmer needs to make sure it stops at the right time or it could run forever! The two main methods to end a loop are using a counter or a rogue value.

Using a count

A count is a way of keeping track of how many times something has happened in your program. A counter variable is used to do this.

Python example

#The loop runs 10 times. After each loop the counter goes up by 1 count = 0 while count 10 print(“Looping…”) count = count + 1 #Without the counter, the loop would run forever.

Using a rogue value

A rogue value is a special value that tells the program to stop. It’s a value that would not normally appear in the data that the program is processing. A rogue value is often used when reading user-entered data, especially in input .

Python example:

#the user will continue to enter grades until they enter -1 grade = int(input(“Enter a grade or -1 to stop the program”)) while grade != -1: print(“You entered:”, grade) grade = int(input(“Enter a grade or -1 to stop the program”)) #-1 is the rogue value as you would not expect a grade to be -1. It is used as a signal to stop asking for more grades