4.1.1.2, 4.1.1.3, 4.1.1.6 - Variables, constants and arithmetic (C#)

4.1.1.2, 4.1.1.3, 4.1.1.6 - Variables, constants and arithmetic (C#)

Use declarations, assignments and sequence to keep track of a program's state (4.1.1.2). Learn the arithmetic operations in 4.1.1.3, then use named constants to express fixed decisions clearly (4.1.1.6).

C# route status: incomplete: runnable support pending. All code work here is written C# with no Run control; complete onscreen Paper 1 preparation still requires runnable support.

Declaration, assignment and sequence

A variable gives a name and type to a value that the program can change.

int seats = 8; declares seats and initialises it to 8. A later seats = seats - 3; is an assignment: evaluate the right-hand expression using the current value, then replace the value on the left. It is not a mathematical equation stating that both sides are always equal. = assigns; == compares equality.

In sequence, statements execute in their written order. Consider this complete program:

C#
using System;

class Program
{
    static void Main()
    {
        int seats = 8;
        int sold = 3;
        seats = seats - sold;
        sold = sold + 1;
        Console.WriteLine(seats);
        Console.WriteLine(sold);
    }
}

The first assignment changes seats to 5; the next changes sold to 4. Increasing sold does not automatically recompute seats, so the output lines are 5 and 4. Swap those assignments and seats becomes 4. A trace table records the value after each statement, rather than guessing from the final expression.

An identifier is a name such as seats. C# identifiers are case-sensitive, cannot contain spaces and cannot start with a digit. remainingSeats conveys purpose; x usually does not. Meaningful names make conditions, changes and mistakes easier to understand, especially for another maintainer. A declared local variable must receive a value before it is read.

The three imperative combining principles are sequence, selection (choose a branch) and iteration (repeat statements). A subroutine is a named reusable block. C# calls these methods: a procedure-like void method returns no value; a function-like method returns a value. Console.WriteLine(seats) is a call to an existing method, rather than an assignment. Later lessons teach selection, both pre/post-test iteration, nested structures and methods in depth; these constructs still use declaration and assignment to change state.

Named constants

A named constant records a fixed value whose purpose should be visible wherever it is used.

Constant

A named value that cannot be reassigned during program execution.

const int SeatsPerRow = 12; declares a C# compile-time constant. Its initialiser must be a constant expression: a literal or a calculation from other constants, rather than keyboard input. A variable such as int rows = 4; can later be reassigned; SeatsPerRow = 10; is not allowed.

int capacity = rows * SeatsPerRow; communicates the calculation better than rows * 12. If the fixed configuration changes for a later version of the program, changing one declaration updates every use. This avoids inconsistent repeated literals and makes the program easier to read and maintain. It does not mean that the student or user can change the constant while that program is executing.

Choose a meaningful name for the role: SecondsPerMinute is clearer than Number60. Constants are useful for conversion factors, fixed limits and agreed rates; live values such as a customer's current balance need variables.

Continue this lesson.

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

Create free account