4.1.1.2, 4.1.1.3, 4.1.1.6 - Variables, constants and arithmetic (VB.NET)

4.1.1.2, 4.1.1.3, 4.1.1.6 - Variables, constants and arithmetic (VB.NET)

Learn to track values through a sequence of statements, use named constants and choose arithmetic that matches the required result. VB.NET questions in this route use written source; the route is incomplete: runnable support pending.

Declaration, assignment and sequence

A variable is a named store whose value can change during execution; assignment replaces its current value with the result of an expression. Read the right-hand side using the values at that moment, then store the result on the left. Assignment is a state change, not a mathematical equation that remains true forever.

The following is a complete VB.NET console program. Module Program contains the program, and Sub Main is the starting procedure. Statements in its body run in sequence, from top to bottom.

VB.NET
Option Strict On
Imports System

Module Program
    Sub Main()
        Dim boxes As Integer = 4
        Dim itemsPerBox As Integer = 6
        Dim totalItems As Integer = boxes * itemsPerBox
        boxes = boxes + 1
        Console.WriteLine(totalItems)
        Console.WriteLine(boxes)
    End Sub
End Module

The output is 24 followed by 5. Changing boxes does not automatically recalculate totalItems; the multiplication was evaluated when that assignment occurred. boxes += 1 is a compact way to perform the same update. Choose meaningful identifiers such as itemsPerBox: they make the stored meaning and unit clearer than a or x, helping maintenance and debugging. Consistent naming does not change the arithmetic.

Imperative programs combine sequence with selection (choose a path) and iteration (repeat a path). A named subroutine groups statements for reuse: a Sub performs a procedure, while a Function returns a value. For example, calling Console.WriteLine asks a procedure to output a value. Later lessons develop branches, loops and user-defined subroutines; this lesson establishes how their individual assignments change state.

Named constants and maintenance

A constant is a named value that cannot be reassigned during execution; use it for a value that the program treats as fixed. A variable can be assigned again. The distinction concerns whether the name's stored value may change, rather than whether a value happens to remain unchanged in one run.

VB.NET
Const ItemsPerPack As Integer = 8
Dim packsOrdered As Integer = 3
Dim itemsOrdered As Integer = packsOrdered * ItemsPerPack

ItemsPerPack names the meaning of 8. If packaging changes for a future version, editing one declaration updates every use, reducing inconsistent edits. A named constant also avoids an unexplained magic number, so someone reading the expression can understand the rule. It protects against accidental reassignment in the program. Const requires a compile-time constant expression; a value read from a user belongs in a variable.

Do not turn a changing stock level into a constant just because it begins at 100. Conversely, using a mutable variable for a fixed conversion factor leaves an unnecessary way to damage the rule. Scope also matters: a declaration inside Sub Main is available there; a declaration at module level can be used by that module's procedures.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account