CodeIEB
Theory Notes/🗄️ Topic 4: Data & Information Management, Solution Development/10.4.8
10.4.8Grade 10

Selection & Looping Algorithms

The control-flow structures that make programs actually make decisions and repeat work — the true engine of every algorithm you'll write.

Selection structures (making decisions):

StructureUse when
ifOne condition, one action if true
if … elseTwo possible outcomes, only one branch runs
switch / caseChoosing between many specific, discrete values of one variable — often cleaner than a long if/else chain

Example

int day = 3; switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; default: System.out.println("Another day"); }

Nested selection (if / else if / else) as a flowchart — only one path is ever taken.

Looping structures (repeating actions):

LoopBest forCheck timing
forA known/counted number of repetitionsPre-check (condition tested before each iteration)
whileAn unknown number of repetitions, based on a conditionPre-check (may run zero times if condition starts false)
do … whileWhen the loop body must run at least oncePost-check (condition tested after each iteration)
repeat … untilSimilar to do-while conceptually (used in pseudocode)Post-check

Choosing for vs while: use a counting loop (for) when you know exactly how many times to repeat; use a condition loop (while) when repetition depends on something happening (e.g. user input, a value being found).

Classic algorithms you must be able to code from scratch:

  • Finding the smallest/largest value in a set of inputs.
  • Calculating a sum and average.
  • Finding factors and multiples of a number.
  • Swapping two values (needs a temporary third variable).
  • Isolating the digits of an integer (using % 10 and / 10 repeatedly).

Example

// Swapping two values needs a temporary variable int temp = a; a = b; b = temp;

Nested selection and nested loops — a selection statement or loop placed entirely inside another. Each nested loop needs its own independent counter variable.

Use methods to abstract away complex nested logic — wrapping a complicated nested loop in a well-named method makes the calling code far more readable, which also improves code efficiency by avoiding duplication if that logic is needed more than once.

💡 Exam Tip

For 'isolating digits' problems: `number % 10` gives the last digit, and `number / 10` (integer division) removes that last digit, ready to repeat for the next digit.