🧭 Week 2 – Data Types, Variables, and Operators
Course: Programming Fundamentals (COMP1112)
Credit Hours: 4 (3 + 1)
Duration: 3-hour Lecture + 3-hour Lab
CLO Mapping: CLO-2 — Apply basic programming concepts
🔹 1. Introduction
Programming involves handling data — storing, processing, and producing meaningful output.
To do this, we use variables, data types, and operators.
This week introduces:
• The building blocks of program data handling
• Variable declaration and scope
• Arithmetic, relational, and logical operations
• Operator precedence and expression evaluation
🔹 2. Variables – The Building Blocks of a Program
Definition:
A variable is a named memory location used to store data temporarily during program execution.
Think of a variable as a box with a label that can hold a value, which can be changed later.
Syntax Comparison
Language Declaration Example Meaning
C++ int age = 20; Declares integer variable “age”
Python age = 20 Creates variable dynamically
Rules for Naming Variables
1. Must start with a letter or underscore (_)
2. Can contain letters, digits, underscores
3. Case-sensitive (Score ≠ score)
4. Avoid using reserved keywords (int, float, etc.)
5. Use meaningful names — e.g., totalMarks, averageAge
Example – Multiple Variable Declarations
C++
int a = 5, b = 10;
float avg = (a + b) / 2.0;
cout << "Average: " << avg;
Python
a, b = 5, 10
avg = (a + b) / 2
print("Average:", avg)
🔹 3. Data Types – The Nature of Data
Every variable has a type that defines what kind of data it can store and how much memory it uses.
C++ Data Types
Type Size (bytes) Example Description
int 4 int age = 18; Whole numbers
float 4 float price = 25.75; Decimal numbers
double 8 double avg = 56.234; Large precision decimals
char 1 char grade = 'A'; Single character
bool 1 bool status = true; Logical value (true/false)
Python Data Types
Type Example Description
int age = 18 Whole number
float pi = 3.14 Decimal value
str name = "Ali" String (sequence of characters)
bool flag = True Logical value
list marks = [90, 85, 70] Collection of data values
Example – Type Casting
C++
int a = 5;
double b = 2;
double result = a / b; // Automatic conversion
cout << result; // Output: 2.5
Python
a = 5
b = 2
result = a / b # Python automatically converts to float
print(result) # Output: 2.5
🔹 4. Constants – Fixed Values
A constant holds a value that cannot be changed during program execution.
C++ Example
const float TAX_RATE = 0.05;
cout << "Tax Rate = " << TAX_RATE;
Python Example
TAX_RATE = 0.05 # Conventionally written in uppercase
print("Tax Rate =", TAX_RATE)
🔹 5. Operators – Performing Operations on Data
Operators act as symbols that perform actions on variables and values.
Types of Operators
Category Description
Arithmetic Perform mathematical operations
Relational Compare values
Logical Combine conditions
Assignment Assign values
Increment/Decrement Change values by one
Miscellaneous Include operations like modulus, etc.
🔹 6. Arithmetic Operators
Operator Meaning Example (C++) Example (Python)
+ Addition a + b a + b
- Subtraction a - b a - b
* Multiplication a * b a * b
/ Division a / b a / b
% Modulus (remainder) a % b a % b
Example – Arithmetic Operations
C++
int a = 15, b = 4;
cout << "Sum = " << a + b << endl;
cout << "Remainder = " << a % b;
Python
a, b = 15, 4
print("Sum =", a + b)
print("Remainder =", a % b)
🔹 7. Relational Operators
Used to compare two values; the result is true or false.
Operator Meaning Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater or equal a >= b
<= Less or equal a <= b
Example – Comparison
C++
int a = 10, b = 5;
cout << (a > b); // Output: 1 (true)
Python
a, b = 10, 5
print(a > b) # Output: True
🔹 8. Logical Operators
Operator Meaning Example (C++) Example (Python)
&& / and True if both conditions true (x > 5 && y < 10) (x > 5 and y < 10)
` /or` True if at least one condition true
! / not Negates condition !(x > 5) not(x > 5)
Example – Decision Check
C++
int marks = 78;
if (marks >= 50 && marks <= 100)
cout << "Pass";
else
cout << "Fail";
Python
marks = 78
if marks >= 50 and marks <= 100:
print("Pass")
else:
print("Fail")
🔹 9. Assignment & Compound Operators
Assignment assigns values, compound operators simplify operations.
Operator Meaning Example
= Simple assignment a = 10
+= Add and assign a += 5
-= Subtract and assign a -= 3
*= Multiply and assign a *= 2
/= Divide and assign a /= 4
Example
C++
int x = 5;
x += 3;
cout << x; // Output: 8
Python
x = 5
x += 3
print(x) # Output: 8
🔹 10. Increment and Decrement Operators
Operator Description C++ Example Python Equivalent
++ Increase by 1 i++ or ++i i += 1
-- Decrease by 1 i-- or --i i -= 1
C++
int i = 10;
i++;
cout << i; // Output: 11
Python
i = 10
i += 1
print(i) # Output: 11
🔹 11. Operator Precedence and Associativity
Defines which operator executes first when multiple appear in an expression.
Precedence Order (High → Low):
1. Parentheses ()
2. Multiplication / Division / Modulus
3. Addition / Subtraction
4. Relational
5. Logical AND, Logical OR
Example
Expression:
3 + 5 * 2 > 10 and 4 < 6
Step-by-step evaluation:
1. 5 * 2 → 10
2. 3 + 10 → 13
3. 13 > 10 → True
4. 4 < 6 → True
5. True and True → True
🔹 12. Practical Coding Examples
Example 1: Check if a Number is Even or Odd
C++
int num;
cout << "Enter a number: ";
cin >> num;
if (num % 2 == 0)
cout << "Even";
else
cout << "Odd";
Python
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
Example 2: Maximum of Two Numbers
C++
int a, b;
cin >> a >> b;
if (a > b)
cout << "A is greater";
else
cout << "B is greater";
Python
a = int(input("Enter A: "))
b = int(input("Enter B: "))
if a > b:
print("A is greater")
else:
print("B is greater")
Example 3: Student Grade Evaluation
C++
int marks;
cout << "Enter marks: ";
cin >> marks;
if (marks >= 80)
cout << "A Grade";
else if (marks >= 60)
cout << "B Grade";
else
cout << "C Grade";
Python
marks = int(input("Enter marks: "))
if marks >= 80:
print("A Grade")
elif marks >= 60:
print("B Grade")
else:
print("C Grade")
🔹 13. Lab Activities (3 Hours)
Objectives:
• Practice using variables and constants
• Apply arithmetic, relational, and logical operators
• Evaluate precedence and short expressions
Lab Tasks:
1. Write a program to input two numbers and perform all arithmetic operations.
2. Create a “Temperature Converter” (Celsius ↔ Fahrenheit).
3. Check whether a number is divisible by both 3 and 5.
4. Compare three numbers and print the largest.
5. Bonus: Create a calculator that evaluates multiple operations based on user choice.
🔹✅ 14. Summary
✅
Variables hold data in memory.
✅
Data types define kind and size of stored data.
✅
Operators perform calculations and logical decisions.
✅
Operator precedence determines evaluation order.
Practice improves understanding of syntax and flow.
🔹 15. Recommended Reading
• Starting Out with Programming Logic & Design by Tony Gaddis
• C How to Program by Paul & Harvey Deitel
• Problem Solving and Program Design in C++ by Hanly & Koffman