understanding-variables-in-programming

Champak's implementation of Bubble Sort

Understanding Variables in Programming

Posted by Champak Roy • Tags: programming, fundamentals, variables

Introduction

Variables are the building blocks of any programming language. They act like containers that store data — whether it’s a number, a piece of text, or something more complex. Think of variables as labeled boxes where you can keep information to use later in your program.

Importance

Without variables, programs would not be flexible. Variables allow programs to take user input, perform calculations, and store results. From calculators to banking systems to AI, variables make it possible to handle changing data and dynamic operations.

Topics

1. What is a Variable?

Variable concept illustration
Variables are named storage locations in memory.

A variable is a symbolic name for a memory location. For example, x = 10 means variable x stores the number 10.

Pseudocode

DECLARE x AS INTEGER
SET x = 10
PRINT x

Dry Run

StepActionValue of x
1Declare xundefined
2Assign 10 to x10
3Print x10 (output)

2. Types of Variables

Variable types illustration
Integers, floats, strings, booleans — different types for different needs.

Variables may hold different types of data depending on the language:

  • Integer (e.g., int x = 5)
  • Float/Double (e.g., float y = 3.14)
  • String (e.g., name = "Shubham")
  • Boolean (e.g., isOn = true)

Pseudocode

DECLARE age AS INTEGER
DECLARE pi AS FLOAT
DECLARE name AS STRING
SET age = 22
SET pi = 3.14
SET name = "Shubham"
PRINT age, pi, name

Dry Run

VariableTypeValue
ageInteger22
piFloat3.14
nameStringShubham

3. Scope of Variables

Scope defines where a variable can be accessed:

  • Local variables: Declared inside a function.
  • Global variables: Declared outside, accessible everywhere.
  • Block variables: Exist only inside a block (e.g., inside loops).

Pseudocode

DECLARE g AS INTEGER
SET g = 100

FUNCTION demo()
   DECLARE l AS INTEGER
   SET l = 10
   PRINT g
   PRINT l
END FUNCTION

Dry Run

ScopeVariableValue
Globalg100
Inside demo()l10

4. Constants

Sometimes values should never change during program execution. These are called constants, e.g., PI = 3.14159. In many languages you use a special keyword (like final in Java) or conventions (all caps) to mark constants.

Pseudocode

DECLARE CONSTANT PI = 3.14159
DECLARE radius AS FLOAT
SET radius = 5
SET area = PI * radius * radius
PRINT area

Dry Run

StepCalculationResult
1PI3.14159
2radius5
3area = PI * 5 * 578.54

Complexity Analysis

Working with variables is usually very efficient: most operations are constant time O(1). Summary:

  • Declare: O(1)
  • Assign: O(1)
  • Access/Read: O(1)
  • Scope resolution: Typically O(1), though some languages search nested scopes in order.
  • Memory: Depends on type/size but access is still O(1).

Conclusion: Variable operations (create, assign, read, update) are O(1).

Interactive JavaScript Demo — Play with Variables

Try assigning values, swapping variables without a third variable, or incrementing them. The demo shows results plus the equivalent JavaScript snippet you can copy.

A = 10
B = 20

Uses and Spin-offs

  • Storing user data like names, passwords, scores.
  • Performing mathematical operations.
  • Configuring system values (limits, tax rates, etc.).
  • Supporting complex data structures like arrays and objects.
  • Enabling dynamic features in modern apps.

History

Early programming languages like FORTRAN (1957) used variables as direct representations of memory locations. Over time, languages abstracted variables to hide memory details, making programming easier and safer.

Interview Questions

Q1: What is a variable?

Answer: A variable is named storage in memory that holds data values which may change during execution.

Q2: Difference between local and global variables?

Answer: Local variables are declared inside functions (accessible only there). Global variables are declared outside functions and are accessible throughout the program.

Q3: What is variable scope?

Answer: Scope defines where a variable is valid — local, global, or block scope.

Q4: What is a constant?

Answer: A constant is a value that, by definition or language support, cannot be changed once set.

Q5: Can variables of the same name exist in different scopes?

Answer: Yes — a local variable can shadow a global variable with the same name; they are separate.

MCQs

1. Which best defines a variable?
  1. A. Fixed value
  2. B. Container for data
  3. C. Loop
  4. D. Condition
2. Which type stores true/false values?
  1. A. Integer
  2. B. Boolean
  3. C. String
  4. D. Float
3. Keyword for constants in Java?
  1. A. final
  2. B. const
  3. C. constant
  4. D. static
4. Default value of int in Java?
  1. A. 0
  2. B. null
  3. C. undefined
  4. D. garbage
5. Operator for assignment?
  1. A. ==
  2. B. =
  3. C. :=
  4. D. =>
6. Scope of variables inside a function?
  1. A. Global
  2. B. Local
  3. C. Universal
  4. D. External
7. Which is a valid Python variable name?
  1. A. 2name
  2. B. my-name
  3. C. name2
  4. D. class
8. Shared among all objects in Java?
  1. A. Instance variable
  2. B. Local variable
  3. C. Static variable
  4. D. Global variable
9. Correct JS variable declaration?
  1. A. variable x = 10;
  2. B. var x = 10;
  3. C. x := 10;
  4. D. int x = 10;
10. Which is a dynamically typed language?
  1. A. C
  2. B. Java
  3. C. Python
  4. D. C++

Assignments

Assignment 1

Swap two variables without using a third variable.

Assignment 2

Declare int, float, and string variables and print their values.

Assignment 3

Create a program that asks for your name and age, then prints them.

Assignment 4

Demonstrate difference between local and global variables.

Assignment 5

Calculate the area of a circle using a constant PI.

Assignment 6

Declare a boolean variable and use it in an if-else condition.

Assignment 7

Take two numbers as input and print their sum.

Assignment 8

Show variable shadowing using same name in local and global scope.

Assignment 9

Store 5 numbers in variables and print their average.

Assignment 10

Update a variable inside a loop and print in each iteration.

Future Study Links

0 Comments

Post a Comment

0 Comments