Technology · Grade 7

What is an algorithm, and how does it work?

When a game shows your best score, how does it pick that number from all the rounds you played?

Start with the goal

Start with the goal

An algorithm is a set of clear steps for solving a problem. The input is what you give it, like a list of scores. The output is the result you want: in this case, the highest score.

Keep one score

Keep one score

Suppose your scores are 8, 12, and 5. Start by saving the first score, 8, as your best so far. This gives you a number to compare the next score with.

Make a choice

Make a choice

A condition is a question with a yes or no answer. Ask, “Is the next score bigger than my best so far?” If yes, save the new score instead; if no, keep the old one. Since 12 is bigger than 8, your best so far becomes 12.

Repeat until done

Repeat until done

A loop repeats a step, so you can use the same comparison for every score left on the list. The last score, 5, is smaller than 12, so keep 12. Once no scores are left to check, stop and report 12.

The whole idea

An algorithm turns a goal into steps you can follow. To find the highest score, keep the best one seen so far and update it only when you find a bigger one.

Questions students ask

Is an algorithm the same as code?

No. The algorithm is the plan. Code expresses that plan in a language a computer can run.

Can two algorithms solve the same problem?

Yes. You could sort the scores from biggest to smallest and take the first one. If all you need is the highest score, you don't need to put the rest in order.

What if I haven't played a round yet?

An empty list has no highest score. The game can check for that first and show “No scores yet” instead of trying to read the first score.

Check yourself

For scores of 9, 3, and 7, what is the output?

The output is 9, the highest score.

Your best so far is 10 and the next score is also 10. Do you need to update it?

No, because the new score is not bigger than the saved score.

Why not stop at the first score that beats your saved score?

A later score might be even bigger.

Snap your own page

Stuck on a page of your own? Take a photo and Slime makes it easy, without giving away the answer.

Snap your own page

More topics

This topic in other languages: 한국어日本語

Written by Slime, an AI. It can be wrong: check with your textbook or teacher.