Notes on Variables in Computer Science
Introduction:
Variables are fundamental elements in computer science and programming. They play a crucial
role in storing and manipulating data during program execution. This set of notes will provide an
overview of variables, their types, declaration, assignment, and scope in computer science.
1. Definition:
- A variable is a named location in computer memory that holds a value, which can be modified
during program execution.
- It acts as a placeholder or container for storing data, allowing programmers to manipulate and
retrieve values as needed.
2. Variable Types:
- Primitive Types: Basic data types provided by programming languages, including integers,
floating-point numbers, characters, and booleans.
- Composite Types: More complex data types constructed from primitive types, such as arrays,
strings, structures, and classes.
3. Variable Declaration:
- Declaration is the process of introducing a new variable to the program.
- It involves specifying the variable's name and type to allocate memory accordingly.
- Syntax: `<type> <variable_name>;`
4. Variable Assignment:
- Assignment involves assigning a value to a variable after declaration.
- Syntax: `<variable_name> = <value>;`
5. Variable Initialization:
- Initialization is the process of assigning an initial value to a variable at the time of declaration.
- Syntax: `<type> <variable_name> = <value>;`
6. Variable Scope:
- Scope defines the portion of the program where a variable is accessible and can be referred
to.
- Local Scope: Variables declared within a specific block or function, accessible only within that
block.
- Global Scope: Variables declared outside of any specific block, accessible throughout the
program.
- Scope resolution: The process of determining which variable to use when there are variables
with the same name but different scopes.
7. Variable Naming Conventions:
Introduction:
Variables are fundamental elements in computer science and programming. They play a crucial
role in storing and manipulating data during program execution. This set of notes will provide an
overview of variables, their types, declaration, assignment, and scope in computer science.
1. Definition:
- A variable is a named location in computer memory that holds a value, which can be modified
during program execution.
- It acts as a placeholder or container for storing data, allowing programmers to manipulate and
retrieve values as needed.
2. Variable Types:
- Primitive Types: Basic data types provided by programming languages, including integers,
floating-point numbers, characters, and booleans.
- Composite Types: More complex data types constructed from primitive types, such as arrays,
strings, structures, and classes.
3. Variable Declaration:
- Declaration is the process of introducing a new variable to the program.
- It involves specifying the variable's name and type to allocate memory accordingly.
- Syntax: `<type> <variable_name>;`
4. Variable Assignment:
- Assignment involves assigning a value to a variable after declaration.
- Syntax: `<variable_name> = <value>;`
5. Variable Initialization:
- Initialization is the process of assigning an initial value to a variable at the time of declaration.
- Syntax: `<type> <variable_name> = <value>;`
6. Variable Scope:
- Scope defines the portion of the program where a variable is accessible and can be referred
to.
- Local Scope: Variables declared within a specific block or function, accessible only within that
block.
- Global Scope: Variables declared outside of any specific block, accessible throughout the
program.
- Scope resolution: The process of determining which variable to use when there are variables
with the same name but different scopes.
7. Variable Naming Conventions: