Write pseudocode statements for: the declaration and initialisation of constants

Resources | Subject Notes | Computer Science

11.1 Programming Basics: Declaration and Initialization of Constants

This section details how to declare and initialize constants in a programming language using pseudo-code. Constants are named values that do not change during the execution of a program. They are useful for representing fixed values like the value of pi or physical constants.

Declaration and Initialization

In pseudo-code, constants are typically declared using a keyword that indicates their immutability. The initialization happens at the time of declaration. Here's the general syntax:


CONSTANT constant_name = constant_value;

The `CONSTANT` keyword (or a similar keyword depending on the pseudo-code style) signifies that the variable `constant_name` is a constant and its value is set to `constant_value` during its declaration.

Examples

Let's illustrate this with some examples:

Example 1: Mathematical Constant

Declare a constant for the value of pi.

  • Pseudo-code:
    1. DECLARE pi AS CONSTANT
    2. SET pi TO 3.14159
  • Explanation: This declares a constant named 'pi' and assigns it the value 3.14159. This value will remain 3.14159 throughout the program.

Example 2: Physical Constant

Declare a constant for the speed of light.

  • Pseudo-code:
    1. DECLARE speed_of_light AS CONSTANT
    2. SET speed_of_light TO 299792458
  • Explanation: This declares a constant named 'speed_of_light' and assigns it the value 299792458.

Example 3: Simple Numeric Constant

Declare a constant for a fixed value.

  • Pseudo-code:
    1. DECLARE max_attempts AS CONSTANT
    2. SET max_attempts TO 3
  • Explanation: This declares a constant named 'max_attempts' and assigns it the value 3. This value represents the maximum number of attempts allowed for a certain operation.

Table Summary

Keyword Declaration Syntax Example Description
CONSTANT CONSTANT constant_name = constant_value; CONSTANT PI = 3.14159; Declares a named value that cannot be changed after initialization.
Other (e.g., const) const constant_name = constant_value; const GRAVITATIONAL_CONSTANT = 9.81; Similar to 'CONSTANT', used in some programming languages.

Important Considerations

Using constants improves code readability and maintainability. If a fixed value needs to be changed, it only needs to be updated in one place (the constant declaration) rather than searching throughout the code.