3.2.3 - Arithmetic and division (VB.NET)

3.2.3 - Arithmetic and division (VB.NET)

Build arithmetic expressions and choose between real division, integer division and remainder in VB.NET. Written native answers have no Run; an optional Python demonstration explores the calculations.

Arithmetic expressions

Arithmetic expressions combine numeric values to calculate a result before assignment stores it.

VB.NET uses + for addition, - for subtraction and * for multiplication. If a cafe sells 3 meals at 7 pounds and refunds 2 pounds, the calculation is:

VB.NET
Dim quantity As Integer = 3
Dim price As Integer = 7
Dim refund As Integer = 2
Dim charge As Integer = quantity * price - refund
Console.WriteLine(charge)

Multiplication happens before subtraction: 3 × 7 = 21, then 21 − 2 = 19. Brackets deliberately change grouping. quantity * (price - refund) is 3 × 5 = 15: that expression deducts the refund from every meal's price, which is a different rule. An expression must represent the stated problem, not merely use the right values.

Choose a real type when a result may be fractional: Dim charge As Double = 3 * 7.5 - 2 gives 20.5. Do not use quoted numeric strings in arithmetic. Work out one known example by hand before trusting a program's output.

Real division, integer division and remainders

Different division operations answer different questions about sharing and grouping.

VB.NET expressionResultMeaning for 11 items and groups of 2
11 / 25.5Real quotient
11 \ 25Complete groups
11 Mod 21Items left over

The backslash \ is VB.NET integer division, distinct from /. Use integer operands for the grouping examples here. AQA pseudocode calls these operations DIV and MOD; write native VB.NET symbols when answering a VB.NET question.

For non-negative whole-number items and a positive group size:
items = (complete groups × group size) + remainder.

For 11 items, 5 * 2 + 1 reconstructs 11. The remainder is smaller than the positive group size. Mod 2 = 0 can identify an even non-negative integer. A mean requires real division: 11 points across 2 games has mean 5.5, not 5. Store that in a Double. Giving an integer destination a fractional result can lose the desired information through conversion.

Optional Python demonstration — grouping. Predict then run. Change items to 12 and compare the outputs. Python uses // and % for these non-negative integer examples; those are not VB.NET's written operators.

Optional Python demonstration: quotient and remainder

{"exercise_id":"aqa8525-vbnet-division-demo","task_version":"1","runtime_profile":"python-console-v1","original_filename":"division_demo.py","file_mode":"fresh","code":"items = 11\ngroup_size = 2\nprint(items / group_size)\nprint(items // group_size)\nprint(items % group_size)\n","instructions":"Predict all three outputs, then run. Change items = 11 to items = 12 and run again; explain why the remainder is now zero."}