A loop can count how many collection items match a target.

search A search checks each item until it finds or counts matches.

Search and Count

target
SearchCount.cs
Replay: real traced execution (multi-file project)
using System;

class Program
{
    static void Main()
    {
        string target = "red";
        string[] labels = { "red", "blue", "red", "gold" };
        int matches = 0;

        foreach (string label in labels)
        {
            if (label == target)
            {
                matches++;
            }
        }

        Console.WriteLine($"target={target}");
        Console.WriteLine($"matches={matches}");
    }
}
using System;

class Program
{
    static void Main()
    {
        string target = "blue";
        string[] labels = { "red", "blue", "red", "gold" };
        int matches = 0;

        foreach (string label in labels)
        {
            if (label == target)
            {
                matches++;
            }
        }

        Console.WriteLine($"target={target}");
        Console.WriteLine($"matches={matches}");
    }
}
using System;

class Program
{
    static void Main()
    {
        string target = "green";
        string[] labels = { "red", "blue", "red", "gold" };
        int matches = 0;

        foreach (string label in labels)
        {
            if (label == target)
            {
                matches++;
            }
        }

        Console.WriteLine($"target={target}");
        Console.WriteLine($"matches={matches}");
    }
}
  1. target ← red, matches ← 0

    4{5    static void Main()6    {7        string target→ red = "red"; //@target="blue", "green"8        string[] labels = { "red", "blue", "red", "gold" };9        int matches→ 0 = 0;
  2. foreach (string label in labels)

    pass 1 of 4
    11foreach (string labelred in labels)12{13    if (label == target)
    All 4 passes — pass 1 is the card above
    passlabeltargetmatches
    1redred0 1
    2blue
    3redred1 2
    4gold
  3. matches ← 1

    pass 1 of 2
    12{13    if (labelred == targetred)14    {15        matches→ 1++;16    }
  4. matches ← 2

    pass 2 of 2
    12{13    if (labelred == targetred)14    {15        matches→ 2++;16    }
  5. Console.WriteLine($"target={target}");

    19    Console.WriteLine($"target={targetred}");20    Console.WriteLine($"matches={matches2}");21}
    outputtarget=red
    matches=2
  1. target ← blue, matches ← 0

    4{5    static void Main()6    {7        string target→ blue = "blue";8        string[] labels = { "red", "blue", "red", "gold" };9        int matches→ 0 = 0;
  2. foreach (string label in labels)

    pass 1 of 4
    11foreach (string labelred in labels)12{13    if (label == target)
    All 4 passes — pass 1 is the card above
    passlabeltargetmatches
    1red
    2blueblue0 1
    3red
    4gold
  3. matches ← 1

    12{13    if (labelblue == targetblue)14    {15        matches→ 1++;16    }
  4. Console.WriteLine($"target={target}");

    19    Console.WriteLine($"target={targetblue}");20    Console.WriteLine($"matches={matches1}");21}
    outputtarget=blue
    matches=1
  1. target ← green, matches ← 0

    4{5    static void Main()6    {7        string target→ green = "green";8        string[] labels = { "red", "blue", "red", "gold" };9        int matches→ 0 = 0;
  2. foreach (string label in labels)

    pass 1 of 4
    11foreach (string labelred in labels)12{13    if (label == target)
    All 4 passes — pass 1 is the card above
    passlabel
    1red
    2blue
    3red
    4gold
  3. Console.WriteLine($"target={target}");

    19    Console.WriteLine($"target={targetgreen}");20    Console.WriteLine($"matches={matches0}");21}
    outputtarget=green
    matches=0

Count Matches

  1. The target is red.
  2. The loop checks each label in order.
  3. When a label equals the target, matches increases by one.
  4. Non-matching labels leave the count unchanged. | Label | Matches red? | Count after check | | --- | --- | --- | | red | yes | 1 | | blue | no | 1 | | red | yes | 2 | | gold | no | 2 |

Exercise: SearchCount.cs

Loop through labels, count how many match a target, and print the final match count