Write pseudocode statements for: the declaration and initialisation of constants

Resources | Subject Notes | Computer Science

11.1 Programming Basics: Constants - Pseudo-code

Objective

Write pseudo-code statements for the declaration and initialization of constants.

What are Constants?

Constants are named values that cannot be changed during the execution of a program. They are used to represent fixed values, making code more readable and maintainable. Using constants also helps prevent accidental modification of important values.

Declaration and Initialization in Pseudo-code

In pseudo-code, constants are typically declared using a keyword like CONSTANT or SOLID, followed by the constant's name and its initial value. The value is assigned during the declaration.

Example Pseudo-code

The following examples demonstrate how to declare and initialize constants in pseudo-code:

Step Pseudo-code Explanation
Example 1: Physical Constant CONSTANT PI = 3.14159 Declares a constant named PI and initializes it with the value 3.14159. This represents the ratio of a circle's circumference to its diameter.
Example 2: Conversion Factor CONSTANT METERS_PER_FOOT = 0.3048 Declares a constant named METERS_PER_FOOT and initializes it with the value 0.3048. This is used for converting between meters and feet.
Example 3: Boolean Constant CONSTANT IS_VALID = TRUE Declares a constant named IS_VALID and initializes it with the boolean value TRUE. This can be used to indicate whether a certain condition is valid.
Example 4: String Constant CONSTANT DEFAULT_NAME = \"Unknown\" Declares a constant named DEFAULT_NAME and initializes it with the string value \"Unknown\". This can be used as a default value if a name is not provided.

Important Considerations

  • Constants are typically written in uppercase to distinguish them from variables.
  • Once a constant is defined, its value cannot be changed. Attempting to do so will usually result in a compilation error.
  • Using constants improves code readability and makes it easier to understand the purpose of certain values.