Bee Learnt Logo
Bee Learnt
FeatureBrowse allPricingAboutSupport
Bee Learnt

Bee Learnt gives South African Grade 10–12 learners a cleaner route from classwork to NSC readiness — with parents and tutors supporting the journey.

WhatsApp: 0685537312

Support email: admin@beelearnt.co.za

Learn
FeaturesBrowse subjectsPricing
Get started
Create accountSign inContact support
Company
About Bee LearntAdvertisingContact
Legal
Privacy policyTerms of serviceCookie policyAd choices

© 2026 Bee Learnt. All rights reserved.

CAPS aligned · Grades 10–12 · NSC ready

Privacy choices

Allow analytics and advertising services to help us understand site usage and measure campaigns.

Privacy Policy
  1. Subjects
  2. /
  3. Information Technology
  4. /
  5. Information Technology Grade 12 — Practical
  6. /
  7. Control flow and strings
  8. /
  9. Choosing loops and decision structures
Grade 12 · Practical

Choosing loops and decision structures

Control flow and strings · about 7 min

2 MIN READ
1 study sections · pause after each one and recall the key idea

Use FOR when the required index range is known in advance. A WHILE loop checks its condition before running, so its body can execute zero times. REPEAT checks after the body and therefore executes at least once; it stops when its UNTIL condition becomes true. Every condition-controlled loop needs a change that moves it towards termination.

A nested loop completes the inner loop for each outer iteration. For a rectangular grid, the outer index often represents a row and the inner index a column. Reset row-specific accumulators inside the outer loop but before the inner loop. Use IF for conditions involving ranges or combinations; CASE is useful for discrete ordinal choices. Check thresholds from most restrictive to least restrictive when overlapping conditions are tested in an IF/ELSE chain.

Key points
  • Choose FOR, WHILE or REPEAT for a reason.
  • Trace nested loops.
  • Avoid non-terminating repetition.

Worked example: // iRow, iCol: Integer; sLine: string; redOutput: TRichEdit redOutput.Clear; for iRow := 1 to 3 do begin sLine := ''; for iCol := 1 to iRow do sLine := sLine + '*'; redOutput.Lines.Add(sLine); end; The output has one, two and three stars on separate lines. The inner body executes 1+2+3=6 times. Resetting sLine before the outer loop instead would retain earlier rows and produce the wrong pattern.

COMMON MISTAKE

WHILE continues while its condition is true; REPEAT stops when its condition becomes true. Swapping these conditions can reverse the intended behaviour.

Practice: Write the inner and outer bounds for a rectangle with 4 rows and 6 stars per row. How many stars are appended?

Answer: Outer loop 1 to 4; inner loop 1 to 6. Reset the line for each row and append it after the inner loop. There are 24 star appends and 4 output lines.

Recap: Choose a loop from its start, bounds and stopping rule; place resets at the correct nesting level.


Next

String processing and safe indexing