3.2.2 - Variables, constants and sequence (VB.NET)
Use named storage and statement order to build a small VB.NET calculation. Native code is written without Run; the labelled optional Python demonstration supports experimentation.
Variables and named constants
A program names values so it can retrieve them and express the purpose of a calculation.
Variable
A named storage location whose value can change during program execution.
Dim seatsBooked As Integer = 4 declares an integer variable and gives it an initial value. A later booking can change it to 5. The declaration tells a reader both its purpose and the kind of value it stores.
Dim seatsBooked As Integer = 4
Const MAX_SEATS As Integer = 30
A named constant stores a value that cannot be reassigned during execution. MAX_SEATS expresses a fixed capacity. Its descriptive name makes MAX_SEATS - seatsBooked understandable; a bare 30 gives less explanation. A named constant also lets a programmer change a fixed setting in one declaration when the requirements change. Capital letters are a useful convention for constants, not the source of their constancy: Const enforces it.
Choose a variable for changing stock and a constant for a fixed delivery charge. Do not make a changing total a constant. Both are identifiers attached to values, but their permitted updates differ.
Assignment and sequence
Assignment first evaluates an expression and then replaces the value stored in its destination.
Dim stock As Integer = 12
stock = stock - 3
stock = stock + 2
Console.WriteLine(stock)
The first update reads the old 12, computes 9, then stores 9. The next update reads 9, computes 11, then stores 11. It is possible for the same variable to occur on both sides because assignment is an action, not an algebraic equality to solve. The final output is 11.
Sequence means instructions execute in their specified order. Calculating a bill before the quantity has been obtained gives the wrong flow even if each expression is sensible. For a ticket bill, plan: obtain quantity → calculate quantity × fixed price → display the bill. Write that order, test a known example, then refine if the observed result differs from the expected result.
Optional Python demonstration — preserving old values. Predict both outputs, run, then move previous = stock below the subtraction. Predict again. The change tests when the old value must be captured; it is not a VB.NET solution.
Optional Python demonstration: statement order
{"exercise_id":"aqa8525-vbnet-sequence-demo","task_version":"1","runtime_profile":"python-console-v1","original_filename":"sequence_demo.py","file_mode":"fresh","code":"stock = 12\nprevious = stock\nstock = stock - 3\nprint(previous)\nprint(stock)\n","instructions":"Predict then run. Move previous = stock to immediately after stock = stock - 3; run again and explain why the first displayed value changes."}
Meaningful identifiers
Meaningful identifiers reveal what values and operations represent in the problem.
ticketQuantity, TICKET_PRICE and CalculateCharge name a variable, constant and subroutine respectively. A subroutine is a named group of instructions; its detailed syntax comes later. x, y and DoThing tell a reader little in this billing context. A short row or i can be meaningful as a loop counter, so length alone is not the test.
VB.NET identifiers may contain letters, digits and underscores, but cannot begin with a digit. Use totalCharge, not total charge or 2tickets. Avoid reserved words such as If. Consistent names reduce misunderstandings when another programmer tests or modifies code. Names do not repair incorrect arithmetic: totalCharge = quantity + TICKET_PRICE still adds when the bill requires multiplication.
Use variables for changing state, constants for fixed settings, and names that explain their roles. Check the order of data flow as well as each individual expression.