3.2.2 - Variables, constants and sequence (C#)

3.2.2 - Variables, constants and sequence (C#)

Use named data and the order of statements to build a program that performs a calculation correctly. Written answers use C#; native execution is unavailable here, so the runnable activity is an optional Python demonstration.

Variables and named constants

A program gives data a name so it can refer to the same value later without repeatedly asking the user or repeating a literal.

Variable

A named location whose stored value can change during program execution.

C#
int ticketsSold = 0;
ticketsSold = 8;

The declaration tells C# that ticketsSold stores an integer. The later assignment replaces 0 with 8; there is still one variable. A declaration can give a starting value immediately, as here, or declare a variable before a later assignment.

A named constant represents a value that must not change during execution. Use const when the value is known at declaration:

C#
const int TicketPrice = 6;
int ticketsSold = 8;
int income = ticketsSold * TicketPrice;

TicketPrice communicates the meaning of 6. If the price changes for a new version of the program, the programmer changes its declaration once. An attempted later assignment to TicketPrice is invalid. Use a variable for changing data such as tickets sold; use a constant for this fixed price. C# convention often gives constants an initial capital, but the purpose comes from their declaration, not their spelling.

Assignment and sequence

Statements execute in sequence unless a control structure changes that flow, so a calculation must use values that have already been assigned.

In ticketsSold = ticketsSold + 1;, first evaluate the right-hand expression using the current value, then store the result on the left. If the old value is 8, the new value is 9. Assignment is an instruction, not an algebraic equality.

C#
int ticketsSold = 8;
ticketsSold = ticketsSold + 1;
int income = ticketsSold * 6;
Console.WriteLine(income);

The result is 54. If the income calculation were moved before the increment, it would store 48; increasing ticketsSold afterwards would not automatically recalculate income. Trace each assignment when checking order. Sequence, selection and iteration are the three basic principles for combining imperative statements; this lesson focuses on sequence.

Predict the optional Python demonstration's two output values, then run it. Move the income calculation below the increment and rerun. This changes the dependency order while leaving the arithmetic itself unchanged.

For a real ticket-sales brief, design the calculation order first, write the statements, predict an expected result and compare it with the observed result. If income should include the latest sale, refine the order as in this activity and retest. This small design–write–test–refine cycle checks whether the program meets its purpose, rather than merely whether it runs.

Optional Python demonstration: assignment order

{"code":"tickets_sold = 8\nincome = tickets_sold * 6\ntickets_sold = tickets_sold + 1\nprint(tickets_sold)\nprint(income)","instructions":"Predict both outputs, then run. Move income = tickets_sold * 6 below tickets_sold = tickets_sold + 1 and run again."}

Meaningful identifiers

An identifier names a variable, constant or subroutine, and a useful name helps a reader understand the role of the named item.

ticketsSold, TicketPrice and CalculateIncome describe data or work. Names such as x, z and DoThing force the reader to reconstruct the purpose from surrounding code. Meaningful names make faults easier to find, make maintenance easier and help another programmer use a subroutine correctly.

In C#, an identifier cannot contain a space, cannot begin with a digit and cannot be an ordinary reserved keyword such as int. C# is case sensitive: ticketCount and TicketCount are different identifiers. Choose a consistent style, rather than relying on confusing differences in capitalisation. A comment can explain a decision, but should not be needed to decode every variable's name.

Use a constant for a fixed named rule, a variable for changing data, and arrange assignments so each calculation receives the intended values.