Skip to content

c. Practical Examples

Programming Foundations: Data, Variables and Operators

These examples use a light I do - We do - You do model. Students should try the prompt first, then compare their thinking with the hidden model response.


Example 1 - I do: Choosing appropriate data types

Scenario

A program needs to store:

  • a student's name
  • a year level
  • an average score
  • whether permission has been returned

Try first

Choose the best data type for each value.

Teacher walkthrough

A strong response could use:

  • student name -> String
  • year level -> Integer
  • average score -> Floating point
  • permission returned -> Boolean
Why these choices work
  • Names are text, so a string is suitable.
  • Year levels are whole numbers, so an integer is suitable.
  • Average scores may include decimals, so a floating point type is suitable.
  • A yes/no state is best stored as a Boolean.

Focus: Good programming begins with choosing the right kind of data.


Example 2 - We do: Tracing variable values

Starting code

int quantity = 3;
double price = 4.50;
double total = quantity * price;
quantity = quantity + 2;
double updatedTotal = quantity * price;

Try first

Work out:

  1. the value of total
  2. the updated value of quantity
  3. the value of updatedTotal
One possible model response
  • total = 13.50
  • quantity = 5
  • updatedTotal = 22.50
What this shows
  • variables can be initialised with a starting value
  • variables can later be updated
  • arithmetic operators allow a program to recalculate values as data changes

We do prompt

If you made a mistake, check whether it happened during initialisation, updating, or arithmetic processing.

Focus: Programs depend on variables changing in a predictable way.


Example 3 - We do: Building a condition

Scenario

A program should only accept a score if it is between 0 and 100 inclusive.

Try first

Write a condition that tests this rule.

One possible model response
score >= 0 && score <= 100
Why this works
  • >= and <= are conditional operators
  • && is a logical operator
  • both conditions must be true for the score to be valid

Focus: Logical operators combine conditions; conditional operators compare values.


Example 4 - You do: Improve weak variable choices

Starting code

string a = "Burger";
string b = "3";
double c = 6.50;
double d = b * c;

Your task

Identify:

  1. one poor data type choice
  2. one reason the calculation is a problem
  3. how you would improve the variable names

Retrieval Prompt

Try to explain the problem before fixing the code. That habit is useful in debugging later.


Example 5 - You do: Quick retrieval grid

Complete the table from memory.

Term What does it mean? Example
Integer
Floating point
Boolean
Variable initialisation
Logical operator
Conditional operator

Study Tip

Retrieval works best when you answer first without notes, then correct any gaps afterwards.