Wikis & learning

Skip blank labels and stop after a limit

A loop may need to discard unsuitable records but still keep searching. Skipping the current row and ending the whole scan are separate decisions.

A loop may need to discard unsuitable records but still keep searching. Skipping the current row and ending the whole scan are separate decisions.

Walkthrough

continue moves to the next candidate. break exits after the second accepted label. Counting accepted items, instead of counting every input, preserves the intended display limit.

PHP example

<?php
$shown = 0;
foreach (['', 'Oak', '', 'Ash', 'Pine'] as $name) {
    if ($name === '') { continue; }
    echo $name, PHP_EOL;
    $shown++;
    if ($shown === 2) { break; }
}

Expected output

Oak
Ash

Change and check

Put several empty strings at the beginning. The result should still contain two labels when enough nonempty inputs remain.