2.2 Algorithms and Loops

Aligned outcomes:

SLO 2

Describe the principles of structured programming.

Iteration is the second pillar of structured programming, and this section builds it from parts: a loop's start, condition, and update, traced one round at a time so you can predict exactly how many times a block runs.

SLO 4

Explain what an algorithm is and its importance in computer programming.

Here you meet the algorithm itself — a finite sequence of precise instructions — and see the same one written as a recipe, as JavaScript, and as Python and C++, so you can say what an algorithm is and why a program is only one implementation of it.

Learning Objectives

After this section, you will be able to:

In this section, you will learn to:
  • Define what an algorithm is and give everyday examples.
  • Explain why programs are implementations of algorithms.
  • Describe how loops let a computer repeat instructions without writing them over and over.
  • Trace a simple loop and predict what it outputs.

2.2.1 What Is an Algorithm?

An algorithm is a sequence of precise instructions that operate on data. You already know lots of algorithms from everyday life -- you just did not call them that.

Think about a recipe for chocolate chip cookies:

  1. Preheat the oven to 375 degrees.
  2. Mix flour, sugar, butter, and eggs in a bowl.
  3. Stir in chocolate chips.
  4. Drop spoonfuls onto a baking sheet.
  5. Bake for 10 minutes.

That is an algorithm. It is a step-by-step plan. If you follow the steps exactly, you get cookies. If you skip a step or do them in the wrong order, you get something else.

Notice what the recipe does NOT say: how big a spoonful is, or how hard to stir. A person fills those gaps without thinking. A computer cannot -- every instruction has to be precise enough that there is nothing left to guess. Most beginner bugs are a step that was clear to you and ambiguous to the machine.

Definition 2.2.1: Algorithm

An algorithm is a finite sequence of well-defined, precise instructions that takes some input, performs a computation, and produces an output. Every algorithm must be clear enough that a computer (or a person) can follow it without guessing.

Definition 2.2.1 — An algorithm is a finite sequence of precise instructions that takes an input, performs a computation, and produces an output.

Example 2.2.1: A Morning Algorithm

Write an algorithm for brushing your teeth as a sequence of steps.

Solution
  1. Pick up toothbrush.
  2. Apply toothpaste to bristles.
  3. Turn on faucet.
  4. Wet the toothbrush.
  5. Turn off faucet.
  6. Brush teeth for 2 minutes.
  7. Rinse mouth with water.
  8. Rinse toothbrush.
  9. Put toothbrush away.

Each step is a single, clear instruction. That is what makes it an algorithm.

Try It Now 2.2.1

Write an algorithm (a sequence of steps) for making a peanut butter and jelly sandwich. List at least 5 steps in order.

Solution
  1. Take two slices of bread out of the bag.
  2. Open the peanut butter jar.
  3. Use a knife to spread peanut butter on one slice.
  4. Open the jelly jar.
  5. Use the knife to spread jelly on the other slice.
  6. Press the two slices together.
  7. Cut the sandwich in half.

2.2.2 Programs Implement Algorithms

A computer cannot follow a recipe written in English. It only understands instructions in a programming language. A program is an implementation of an algorithm written in a formal programming language like JavaScript.

Computers can only execute a finite, pre-defined set of instructions exactly as instructed. This is why programming can feel so different from talking to another person. When you tell your friend Marisol "wake me up in 10 minutes," she understands what you mean even if you are vague. A computer needs every detail spelled out.

Definition 2.2.2: Program

A program is an implementation (realization) of an algorithm written in a formal programming language that a computer can execute.

Definition 2.2.2 — A program is an implementation of an algorithm written in a formal programming language a computer can execute.

Example 2.2.2: From Algorithm to Code

Here is an algorithm for deciding what to wear based on temperature:

  1. Check the temperature.
  2. If it is below 60 degrees, wear a jacket.
  3. Otherwise, wear a t-shirt.

Write it as a JavaScript program -- the algorithm's three steps are the three comments below:

Editor
runs in your browser
▶ Press Run to see the output…
Solution
Editor
runs in your browser
▶ Press Run to see the output…

You will see:

Wear a jacket.

The program does exactly what the algorithm says, but in a language the computer understands.

Try It Now 2.2.2

Turn this algorithm into a JavaScript program:

  1. Set a variable score to 75.
  2. If score is 70 or above, print "Passing".
  3. Otherwise, print "Failing".
Editor
runs in your browser
▶ Press Run to see the output…
Solution
Editor
runs in your browser
▶ Press Run to see the output…

You will see:

Passing

2.2.3 Common Ideas Across Languages

Although each programming language is different from all the others, there are still common ideas across all of them. Knowing just a few of these common ideas enables computer scientists to address a wide variety of problems without having to start from scratch every single time.

For example, the abstraction of string data enables programmers to write programs that operate on human-readable letters, digits, punctuation, and spaces without having to manage each character individually. Every major language has strings, numbers, conditionals, and loops -- the building blocks are the same even when the syntax looks different.

If you learn the concepts in one language (JavaScript, in this book), you have already learned most of what you need to pick up another language later. The syntax changes, but the ideas -- variables, conditions, loops, functions -- stay the same.

Example 2.2.3: Same Idea, Different Forms

An algorithm is a plan. It exists before any code does, and the same plan can be written more than one way. Here is "print the numbers 1 through 5" as pseudocode, the plain-language form you met in Section 1.5:

set i to 1
while i is 5 or less
    print i
    add 1 to i

Write that plan as a JavaScript for loop -- each comment is one line to write:

Editor
runs in your browser
▶ Press Run to see the output…

Now write the same plan again as a while loop:

Editor
runs in your browser
▶ Press Run to see the output…
Solution
Editor
runs in your browser
▶ Press Run to see the output…
Editor
runs in your browser
▶ Press Run to see the output…

Three spellings, one algorithm: start at 1, keep going while you are at 5 or below, add 1 each time. The steps are the thing you are choosing; the syntax is how you write them down. That is why it pays to work the plan out before you start typing.

What you will see (either version)
1
2
3
4
5
Try It Now 2.2.3

Run this JavaScript code, which prints "Hello" three times:

for (let count = 1; count <= 3; count++) {
  console.log("Hello");
}

Type it into the editor and run it:

Editor
runs in your browser
▶ Press Run to see the output…

What do you see? Try changing the 3 to 5 and run it again.

Solution

With 3, you see:

Hello
Hello
Hello

With 5, you see:

Hello
Hello
Hello
Hello
Hello

The loop runs the console.log line once for each number from 1 up to the limit.

2.2.4 Introduction to Loops

One of the most powerful ideas in programming is the loop: telling the computer to repeat a set of instructions. Without loops, if you wanted to print the numbers 1 through 100, you would have to write 100 console.log statements. With a loop, you write the instruction once and tell the computer how many times to repeat it.

A loop has three key parts:

  1. Start -- where does the count begin?
  2. Condition -- when should we stop?
  3. Update -- how does the count change each time?

In JavaScript, the for loop puts all three on one line:

Editor
runs in your browser
▶ Press Run to see the output…

The condition is where loops go wrong most often. i <= 5 runs five times; i < 5 runs only four. Changing one character changes the answer, so when a loop prints one too many or one too few, check the condition before anything else.

Those three parts are easier to believe as a picture. A loop is not a special shape -- it is an ordinary decision diamond with one arrow that goes back up.

Figure 2.2.1 The student submitted a flowchart diagram. Mermaid source: ```mermaid flowchart TD n1([Start]) n2[i = 1] n3{"i <= 5"} n4([End]) n5[print i] n6[i = i + 1] n1 --> n2 n2 --> n3 n3 -- no --> n4 n3 -- yes --> n5 n5 --> n6 n6 --> n3 ``` Shape-by-shape walk (6 shapes, 6 arrows): - n1 Terminal (start/end oval) labelled "Start" — goes to "i = 1". - n2 Task (rectangle) labelled "i = 1" — goes to "i <= 5". - n3 Decision (diamond) labelled "i <= 5" — on "no" goes to "End"; on "yes" goes to "print i". - n4 Terminal (start/end oval) labelled "End" — no outgoing arrow. - n5 Task (rectangle) labelled "print i" — goes to "i = i + 1". - n6 Task (rectangle) labelled "i = i + 1" — goes to "i <= 5". no yes Start i = 1 i <= 5 End print i i = i + 1

Figure 2.2.1 — A for loop unfolded. Start, condition, update are three separate boxes; the loop is the arrow returning to the condition.

The condition is tested *before* the body every time, including the very first. The update is a box the arrow passes through on its way back -- which is why changing i <= 5 to i < 5 removes one whole trip around the circuit.

Those three parts are easier to believe as a picture. A loop is not a special shape -- it is an ordinary decision diamond with one arrow that goes back up.

The condition is tested before the body every time, including the very first. The update is a box the arrow passes through on its way back -- which is why changing i <= 5 to i < 5 removes one whole trip around the circuit.

Definition 2.2.3: Loop

A loop is a programming construct that repeats a block of code as long as a specified condition remains true.

Definition 2.2.3 — A loop repeats a block of code as long as a specified condition remains true.

Example 2.2.4: Counting by Twos

A loop does not have to count by ones. Build one that prints the even numbers from 2 to 10 -- each comment is one line to write:

Editor
runs in your browser
▶ Press Run to see the output…
Solution
Editor
runs in your browser
▶ Press Run to see the output…

You will see:

2
4
6
8
10

The update i += 2 adds 2 each time instead of 1. Change it to i += 3 and run it again to see the step size drive the whole sequence.

Try It Now 2.2.4

Write a for loop that prints the numbers 5, 4, 3, 2, 1. (Hint: start at 5, go down by 1 each time, stop when you reach 1.)

Editor
runs in your browser
▶ Press Run to see the output…
Solution
Editor
runs in your browser
▶ Press Run to see the output…

You will see:

5
4
3
2
1

The update i-- subtracts 1 each time instead of adding.

Example 2.2.5: A Loop You Cannot Count in Advance

Every loop so far has known its own size before it started. i = 1 to i <= 5 is five trips, and you can say so without running anything. That kind of loop is called definite: the number of repetitions is settled up front.

Not every task is like that. Sometimes the rule for stopping is clear but the number of steps is not, and you only find out by going.

Here is a famous one. Start with any whole number above 1. If it is even, halve it. If it is odd, triple it and add 1. Keep going until you reach 1.

Follow the rule by hand starting at 6, and write down how many steps it takes before you run anything:

Editor
runs in your browser
▶ Press Run to see the output…

Six goes 3, 10, 5, 16, 8, 4, 2, 1 -- eight steps, and it climbs to 16 on the way to 1, which is more than a little strange for a rule that is supposed to be shrinking things.

Now change n to 7 and run it again. Seven takes sixteen steps and reaches 52. Try 27, which takes 111 steps and climbs past 9000.

That is the difference this example exists to show. Nothing about the number 27 tells you it will take 111 steps. There is no sum you can do first. The loop is indefinite -- the condition decides when to stop, and the only way to learn the count is to run it.

This is why while is written the way it is. A for loop puts start, condition and update on one line because you usually know all three when you write it. A while loop states the condition alone, because it is the only part you are sure of.

Nobody knows whether this rule reaches 1 for every starting

number. It has been checked by computer far past any number you would type in,

and it has always come back to 1, but "always so far" is not a proof and this

has been open since the 1930s. It is a fair thing to find unsettling: you can

write a loop in four lines and not be able to promise it ever stops.

Try It Now 2.2.5

Write a loop that starts at 1000 and halves it, printing each value, for as long as the value stays above 1. Count the trips.

Before running it, predict the count. Halving 1000 repeatedly gives 500, 250, 125, and then it stops dividing evenly -- so decide what you expect to happen once the numbers stop being whole.

Editor
runs in your browser
▶ Press Run to see the output…
Solution
Editor
runs in your browser
▶ Press Run to see the output…

Ten trips, and the tail of the list is not what a prediction usually expects: after 125 comes 62.5, then 31.25, and the values stay fractional the rest of the way down. Halving does not care whether the result is whole.

The loop still ends, because each value is genuinely smaller than the one before and the condition only asks whether it is above 1. That is the shape every indefinite loop needs -- something in the body has to move the condition toward false.

Example 2.2.6: Walking Through a Word

Every loop so far has counted numbers. The counter does not care what you use it for, and one of the most useful things to count through is the characters of a string.

You have both pieces already: .length says how many characters there are, and word[i] reaches the one at position i (Section 1.2.3).

Editor
runs in your browser
▶ Press Run to see the output…

Six lines, numbered 0 to 5. Read the loop header against the string and it is the same shape you already know: start at the first position, keep going while i is still a real position, step on by one.

Note i < word.length and not i <= word.length. The last character of "banana" sits at position 5, so stopping before 6 is exactly right — i <= word.length would run one extra round and print undefined.

Counting through characters is how you answer questions about a word that no single operation answers. Here is how many times a letter appears:

Editor
runs in your browser
▶ Press Run to see the output…

Three. The loop visits every character, the if decides which ones matter, and the counter remembers. That combination — visit everything, test each one, keep a running answer — is the shape of an enormous number of programs, and it is worth recognizing now that you have seen it whole.

2.2.5 Canonical Algorithms

The study of data structures and algorithms focuses on identifying what is known as a canonical algorithm: a well-known algorithm that showcases design principles helpful across a wide variety of problems.

Sorting is a classic example. There are many ways to sort a list of numbers, and each sorting algorithm teaches a different design principle. Some are simple but slow; others are fast but harder to understand. By studying these canonical algorithms, computer scientists learn patterns they can apply to new problems.

In this chapter, rather than focusing on the programming details, we focus on algorithms and the ideas behind them. The specific syntax will change from language to language, but the logic -- the algorithm -- stays the same.

Definition 2.2.4: Canonical Algorithm

A canonical algorithm is a well-known, widely studied algorithm that demonstrates important design principles applicable across many different problems.

Definition 2.2.4 — A canonical algorithm is a well-known algorithm that demonstrates design principles applicable across many different problems.

Example 2.2.7: A Simple Search Algorithm

A canonical algorithm called linear search goes through a list one item at a time until it finds what it is looking for. Build it -- each comment is one line to write:

Editor
runs in your browser
▶ Press Run to see the output…
Solution
Editor
runs in your browser
▶ Press Run to see the output…

You will see:

Found cherry at position 2

The loop checks each item: position 0 is "apple", position 1 is "banana", position 2 is "cherry" -- found it. The break statement stops the loop early.

This algorithm has two exits, and the chart is the clearest way to see the difference between them.

Figure 2.2.2 The student submitted a flowchart diagram. Mermaid source: ```mermaid flowchart TD n1([Start]) n2[found = false] n3[i = 0] n4{"i < items.length"} n5{"items[i] == target"} n6[found = true] n7["print #quot;Found #quot; + target + #quot; at position #quot; + i"] n8{not found} n9[i = i + 1] n10["print target + #quot; not found.#quot;"] n11([End]) n1 --> n2 n2 --> n3 n3 --> n4 n4 -- yes --> n5 n5 -- yes --> n6 n6 --> n7 n7 --> n8 n5 -- no --> n9 n9 --> n4 n4 -- no --> n8 n8 -- yes --> n10 n10 --> n11 n8 -- no --> n11 ``` Shape-by-shape walk (11 shapes, 13 arrows): - n1 Terminal (start/end oval) labelled "Start" — goes to "found = false". - n2 Task (rectangle) labelled "found = false" — goes to "i = 0". - n3 Task (rectangle) labelled "i = 0" — goes to "i < items.length". - n4 Decision (diamond) labelled "i < items.length" — on "yes" goes to "items[i] == target"; on "no" goes to "not found". - n5 Decision (diamond) labelled "items[i] == target" — on "yes" goes to "found = true"; on "no" goes to "i = i + 1". - n6 Task (rectangle) labelled "found = true" — goes to "print "Found " + target + " at position " + i". - n7 Task (rectangle) labelled "print "Found " + target + " at position " + i" — goes to "not found". - n8 Decision (diamond) labelled "not found" — on "yes" goes to "print target + " not found.""; on "no" goes to "End". - n9 Task (rectangle) labelled "i = i + 1" — goes to "i < items.length". - n10 Task (rectangle) labelled "print target + " not found."" — goes to "End". - n11 Terminal (start/end oval) labelled "End" — no outgoing arrow. yes yes no no yes no Start found = false i = 0 i < items.length items[i] == target found = true print "Found " + target +" at position " + i not found i = i + 1 print target + " notfound." End

Figure 2.2.2 — Linear search. The break is the arrow leaving the loop from inside the body, rather than from the condition at the top.

Two different arrows arrive at the not found check. One comes off the top of the loop -- i ran out and nothing matched. The other comes out of the middle of the body: that is the break, taken because we found it and left early. A loop that can only exit from the top can never stop early, however much of the list is left.

This algorithm has two exits, and the chart is the clearest way to see the difference between them.

Two different arrows arrive at the not found check. One comes off the top of the loop -- i ran out and nothing matched. The other comes out of the middle of the body: that is the break, taken because we found it and left early. A loop that can only exit from the top can never stop early, however much of the list is left.

Try It Now 2.2.6

Modify the linear search below. Change the target to "grape" and run it. What happens? Why?

let items = ["apple", "banana", "cherry", "date"];
let target = "cherry";
let found = false;

for (let i = 0; i < items.length; i++) {
  if (items[i] == target) {
    found = true;
    console.log("Found " + target + " at position " + i);
    break;
  }
}

if (!found) {
  console.log(target + " not found.");
}

Type it into the editor and run it:

Editor
runs in your browser
▶ Press Run to see the output…
Solution
Editor
runs in your browser
▶ Press Run to see the output…

You will see:

grape not found.

The loop checks every item in the list. Since "grape" is not there, found stays false, and the if (!found) block runs after the loop finishes.

Problem Set 2.2

2.2.1 Write an algorithm (as a numbered list of steps) for checking whether a number is even or odd.

Solution

Step 1 — Decide what "even" means: A number is even when dividing it by 2 leaves nothing behind, and odd when dividing it by 2 leaves 1 behind. So the whole algorithm rests on one test: what is the remainder after dividing by 2?

Step 2 — Write the steps precisely: Each step has to be a single, clear instruction — no guessing allowed.

  1. Take the number as input.
  2. Divide the number by 2 and keep the remainder.
  3. If the remainder is 0, report "even".
  4. Otherwise, report "odd".

Answer: The algorithm is: (1) take the number, (2) find the remainder when it is divided by 2, (3) if the remainder is 0 report "even", (4) otherwise report "odd".

2.2.2 Turn your algorithm from problem 2.2.1 into a JavaScript program. Use a variable num and an if...else statement.

Editor
runs in your browser
▶ Press Run to see the output…
Solution

Step 1 — Find the JavaScript operator for "the remainder": The remainder step from problem 2.2.1 is written % in JavaScript. The expression num % 2 gives the remainder after dividing num by 2 — either 0 or 1.

Step 2 — Turn steps 3 and 4 into if...else: "If the remainder is 0, report even; otherwise report odd" is exactly an if...else. Use === to compare, because === means "is equal to" (one = would assign instead).

Editor
runs in your browser
▶ Press Run to see the output…

Step 3 — Check it: With num = 7, the expression 7 % 2 is 1, which is not 0, so the else branch runs and the console shows odd. Change num to 8 and 8 % 2 is 0, so it prints even.

Answer: The program above. It stores the number in num, tests num % 2 === 0, and prints "even" or "odd" accordingly.

2.2.3 Write a for loop that prints the numbers 10, 20, 30, 40, 50 to the console.

Editor
runs in your browser
▶ Press Run to see the output…
Solution

Step 1 — Identify the loop's three parts:

  • Start — the first number printed is 10, so begin at i = 10.
  • Condition — the last number printed is 50, so keep going while i <= 50.
  • Update — the numbers go up by 10 each time, so i += 10.

Step 2 — Assemble the for loop:

Editor
runs in your browser
▶ Press Run to see the output…

Step 3 — Trace it to be sure: i is 10 (print), 20 (print), 30 (print), 40 (print), 50 (print), then 60 — and 60 <= 50 is false, so the loop stops. Five numbers, exactly the ones asked for.

Answer: The loop above prints 10, 20, 30, 40, 50.

2.2.4 What does this loop print? Trace it step by step.

Editor
runs in your browser
▶ Press Run to see the output…
Solution

Step 1 — Read the three parts: let i = 0 starts the count at 0. i < 3 keeps the loop going while i is less than 3 — note this is <, not <=, so 3 itself never runs. i++ adds 1 after each round.

Step 2 — Trace one round at a time:

  • i = 0 — is 0 < 3? Yes. Print "Round " + 0Round 0. Then i becomes 1.
  • i = 1 — is 1 < 3? Yes. Print Round 1. Then i becomes 2.
  • i = 2 — is 2 < 3? Yes. Print Round 2. Then i becomes 3.
  • i = 3 — is 3 < 3? No. The loop stops.

Step 3 — Note the two things students expect to be different: The count starts at 0, not 1, and it stops before 3. So the loop runs three times but the highest number printed is 2.

Answer: It prints three lines:

Round 0
Round 1
Round 2

2.2.5 Write a loop that prints every third number from 3 to 30 (3, 6, 9, ... 30).

Editor
runs in your browser
▶ Press Run to see the output…
Solution

Step 1 — Identify the three parts: "Every third number from 3 to 30" means 3, 6, 9, 12, …, 30. So: start at i = 3, condition i <= 30, update i += 3.

Step 2 — Write the loop:

Editor
runs in your browser
▶ Press Run to see the output…

Step 3 — Check the last value: The loop prints 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 — ten numbers. After printing 30, i becomes 33, and 33 <= 30 is false, so it stops. Using i < 30 instead would have dropped the 30, so the <= matters here.

Answer: The loop above prints 3, 6, 9, 12, 15, 18, 21, 24, 27, 30.

2.2.6 Explain in your own words: what is the difference between an algorithm and a program?

Solution

Step 1 — Say what each one is: An algorithm is the plan: a finite sequence of precise, well-defined instructions that takes input, does a computation, and produces output. A program is the implementation of that plan, written in a formal programming language a computer can actually execute.

Step 2 — Point at what makes them different: The algorithm can live in English, in a numbered list, in a diagram, or in your head — it is language-independent. The program has to be written in one specific language, with that language's exact syntax, because the computer only executes a finite, pre-defined set of instructions and cannot fill in anything you left vague.

Step 3 — Use the section's own example: "If the temperature is below 60 degrees, wear a jacket; otherwise wear a t-shirt" is the algorithm. The if (temperature < 60) { … } else { … } block from Example 2.2.2 is one program that implements it — and the for and while versions in Example 2.2.3 are two different programs implementing one and the same algorithm.

Answer: An algorithm is the step-by-step plan for solving a problem, stated precisely but in any form; a program is that algorithm written out in a specific programming language so a computer can run it. One algorithm can be implemented as many different programs.

2.2.7 Write a loop that counts down from 10 to 1 and then prints "Blast off!".

Editor
runs in your browser
▶ Press Run to see the output…
Solution

Step 1 — Set up the countdown loop: Counting down means the update subtracts instead of adds. Start at i = 10, condition i >= 1 (keep going while i is at least 1), update i--.

Step 2 — Put the "Blast off!" line in the right place: The message prints once, after all the counting is finished — so it goes after the loop's closing brace, not inside the loop body. Inside the body it would print ten times.

Editor
runs in your browser
▶ Press Run to see the output…

Step 3 — Check the output: The loop prints 10, 9, 8, 7, 6, 5, 4, 3, 2, 1. Then i becomes 0, 0 >= 1 is false, the loop ends, and the last line runs once.

Answer: The program above prints 10 down to 1 on separate lines, then Blast off!.

2.2.8 Give one example of a canonical algorithm (other than linear search) and describe what it does in one sentence.

Solution

Step 1 — Recall what makes an algorithm "canonical": A canonical algorithm is a well-known, widely studied algorithm that demonstrates design principles useful far beyond the one problem it solves. Sorting and searching algorithms are the classic examples.

Step 2 — Pick one and describe it in a sentence: Binary search — on a list that is already sorted, look at the middle item, and if it is not the target, throw away the half of the list the target cannot be in and repeat on the half that remains.

Step 3 — Say why it is canonical (optional but worth seeing): It teaches the divide-and-conquer principle: cutting the problem in half each round lets binary search check a list of a million items in about twenty comparisons, where linear search might need a million.

Answer: Binary search. On a sorted list it repeatedly checks the middle item and discards the half that cannot contain the target, narrowing the search by half each round. (Bubble sort, merge sort, or Euclid's algorithm for the greatest common divisor are equally good answers.)

Key Terms

Algorithm -- A finite sequence of precise, well-defined instructions that takes input, performs a computation, and produces output.

Program -- An implementation of an algorithm written in a formal programming language that a computer can execute.

Loop -- A programming construct that repeats a block of code as long as a condition remains true.

for loop -- A loop that combines initialization, condition, and update in one line.

Canonical algorithm -- A well-known algorithm that demonstrates design principles applicable across many problems.

String -- A data type representing a sequence of characters (letters, digits, punctuation, spaces).

Linear search -- A canonical algorithm that checks each item in a list one at a time until the target is found or the list ends.