Resources | Subject Notes | Computer Science
Write pseudo-code statements for the declaration and initialization of 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.
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.
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. |