Skip to content

c. Practical Examples

Control Structures and Validation

These examples use a light I do - We do - You do model. The aim is to make students think about program flow and input checking before they look at a model answer.


Example 1 - I do: Spotting a selection logic problem

Starting code

if (temperature < 10)
{
    Console.WriteLine("Very cold");
}

if (temperature < 20)
{
    Console.WriteLine("A little cool");
}
else
{
    Console.WriteLine("Warm");
}

Try first

Before opening the walkthrough, identify:

  1. what could go wrong if temperature is 9
  2. why this structure may produce the wrong output
Teacher walkthrough

If temperature is 9, the program can print both Very cold and A little cool.

The problem is that the first and second checks are separate IF statements. A chained structure is more suitable when only one message should appear.

One possible improvement
if (temperature < 10)
{
    Console.WriteLine("Very cold");
}
else if (temperature < 20)
{
    Console.WriteLine("A little cool");
}
else
{
    Console.WriteLine("Warm");
}

Focus: Structure matters just as much as the condition itself.


Example 2 - We do: Choosing the right loop

Scenario

A program keeps asking for a password until the user enters one that is not blank.

Try first

Decide:

  1. which loop would be most suitable
  2. why that loop fits the task
One possible model response

A WHILE loop or DO/WHILE loop could be suitable because the number of attempts is not known in advance.

A DO/WHILE loop is especially suitable if the program must ask at least once before checking whether the password is valid.

Focus: Loop choice depends on whether repetition count is known and whether the code must run at least once.


Example 3 - We do: Applying validation in the right order

Scenario

A form asks for a student age.

The rules are:

  • the field cannot be blank
  • the value must be numeric
  • the age must be between 15 and 17

Try first

Write the validation order you would use.

One possible model response
  1. Existence check
  2. Type check
  3. Range check
Why this order works
  • Check that something has been entered first.
  • Check that the entered value is the right kind of data next.
  • Only then test whether it falls within the allowed range.

Focus: Range checking only makes sense after the input exists and can be treated as the correct type.


Example 4 - You do: Design a control structure plan

Scenario

A student is creating a simple quiz program.

The program should:

  • ask a question
  • accept an answer
  • check whether it is correct
  • repeat for five questions

Your task

Decide:

  1. where sequence is used
  2. where selection is used
  3. where iteration is used

Retrieval Prompt

Try to explain the job of each control structure in plain English before writing any code.


Example 5 - You do: Retrieval grid

Complete the table from memory.

Term What does it mean? Example use
Sequence
Selection
Iteration
Existence check
Type check
Range check

Study Tip

Retrieval is more effective when you explain the purpose of the idea, not just memorise the label.