Write pseudocode statements for: the declaration of variables
11.1 Programming Basics
Variable Declaration in Pseudocode
Think of a variable as a labelled box 📦 that can hold a value. In pseudocode we first declare the box so the computer knows what kind of value it can store. The general form is:
DECLARE <variable_name> AS <data_type>
Analogy: Declaring a variable is like putting a name tag on a storage box. The data type tells the box how big it should be and what kind of items it can hold (numbers, words, etc.). Without a name tag, the computer would not know which box to use when you later refer to it.
Common Data Types
| Data Type | Description | Example Declaration |
|---|---|---|
| INTEGER | Whole numbers (e.g., 0, 7, -12) | DECLARE age AS INTEGER |
| REAL | Numbers with a decimal point (e.g., 3.14, -0.001) | DECLARE height AS REAL |
| STRING | Sequences of characters (e.g., "Alice", "Hello, world!") | DECLARE name AS STRING |
| BOOLEAN | True or False values | DECLARE isStudent AS BOOLEAN |
Putting It All Together
- Choose a clear, descriptive name for your variable.
- Select the appropriate data type.
- Write the declaration using the
DECLAREkeyword. - Optionally, initialise the variable:
DECLARE counter AS INTEGER = 0.
Exam Tip:
- Always use the
DECLAREkeyword when introducing a new variable. - Specify the data type; omitting it can lead to errors.
- Remember that pseudocode is case‑insensitive, but keep your style consistent.
- Use initialisation only when you know the starting value.
Common Mistakes
- Declaring a variable without a data type.
- Using reserved words (e.g.,
DECLARE,INTEGER) as variable names. - Mixing up data types (e.g., assigning a string to an integer variable).
- Forgetting to initialise variables that will be used in calculations.
Quick Practice
Try declaring the following variables in pseudocode:
- A variable
scorethat holds a whole number. - A variable
temperaturethat holds a real number. - A variable
studentNamethat holds a string. - A variable
hasPassedthat holds a boolean.
Write your answers below and check them against the examples above. Happy coding! 🚀
Revision
Log in to practice.
2 views
0 suggestions