Resources | Subject Notes | Computer Science
Variable declaration is the process of reserving memory space in a computer's memory to store data. In pseudo-code, we use a simple statement to indicate the creation and naming of a variable, along with its initial value (if any).
The general syntax for declaring a variable in pseudo-code is:
DECLARE AS [= ];
Where:
DECLARE
: Keyword indicating the start of a variable declaration.
: The name you choose for the variable (should follow naming conventions).
: Specifies the type of data the variable will hold (e.g., INTEGER, REAL, STRING, BOOLEAN).[= ]
: Optional. Specifies an initial value for the variable. If omitted, the variable is uninitialized.Here are some examples of variable declarations in pseudo-code:
Example 1: Declaring an integer variable.
Pseudo-code | Explanation |
---|---|
DECLARE age AS INTEGER; | Declares a variable named 'age' to store whole numbers (integers). It is uninitialized. |
Example 2: Declaring a real (floating-point) variable with an initial value.
Pseudo-code | Explanation |
---|---|
DECLARE price AS REAL = 19.99; | Declares a variable named 'price' to store decimal numbers (real). It is initialized with the value 19.99. |
Example 3: Declaring a string variable.
Pseudo-code | Explanation |
---|---|
DECLARE name AS STRING = "Alice"; | Declares a variable named 'name' to store text. It is initialized with the string "Alice". |
Example 4: Declaring a boolean variable.
Pseudo-code | Explanation |
---|---|
DECLARE is_valid AS BOOLEAN = TRUE; | Declares a variable named 'is_valid' to store a true/false value. It is initialized to TRUE. |
Important Considerations: