Module 2: Variables, Data Types & Operators

Learn variables, constants, numeric data types, strings, Boolean values, type conversion, arithmetic, comparison and logical operators using C, C++, Java and Python.

CC++JavaPythonComparative Study
Store
Data
Convert
Types
Apply
Operators
Compare
Syntax

2.1 Module Overview

Variables, data types and operators are the building blocks of every program. A variable stores data, a data type defines the kind of data being stored, and operators perform calculations or comparisons.

This module teaches the same concepts using C, C++, Java and Python. Learners will see how syntax changes while programming logic remains the same.

Learning Outcome: Use variables, data types and operators in C, C++, Java and Python through comparative working examples.

2.2 Learning Objectives

  • Declare and initialize variables in C, C++, Java and Python.
  • Use constants to store fixed values.
  • Apply numeric, string and Boolean data types.
  • Perform type conversion.
  • Use arithmetic, comparison and logical operators.
  • Compare syntax differences between all four languages.

2.3 Variables

A variable is a named memory location used to store data.

LanguageVariable Declaration
Cint age = 20;
C++int age = 20;
Javaint age = 20;
Pythonage = 20

Working Example: Store and Display Student Age

C
#include <stdio.h>
int main(){
    int age = 20;
    printf("Student Age: %d", age);
    return 0;
}
C++
#include <iostream>
using namespace std;
int main(){
    int age = 20;
    cout << "Student Age: " << age;
    return 0;
}
Java
public class Main{
    public static void main(String[] args){
        int age = 20;
        System.out.println("Student Age: " + age);
    }
}
Python
age = 20
print("Student Age:", age)
Expected Output:
Student Age: 20

2.4 Variables and Constants

A constant is a value that should not change during program execution.

Working Example: Calculate Circle Area

C
#include <stdio.h>
int main(){
    const float PI = 3.14159;
    float radius = 5;
    float area = PI * radius * radius;
    printf("Area: %.2f", area);
    return 0;
}
C++
#include <iostream>
using namespace std;
int main(){
    const float PI = 3.14159;
    float radius = 5;
    float area = PI * radius * radius;
    cout << "Area: " << area;
    return 0;
}
Java
public class Main{
    public static void main(String[] args){
        final double PI = 3.14159;
        double radius = 5;
        double area = PI * radius * radius;
        System.out.println("Area: " + area);
    }
}
Python
PI = 3.14159
radius = 5
area = PI * radius * radius
print("Area:", area)
Expected Output: Area around 78.54
Note: Python does not enforce constants by default. Constant names are written in capital letters by convention.

2.5 Numeric Data Types

Numeric data types store numbers. Integer stores whole numbers, while float and double store decimal values.

LanguageIntegerFloatDouble
Cintfloatdouble
C++intfloatdouble
Javaintfloatdouble
Pythonintfloatfloat

Working Example: Average Marks

C
#include <stdio.h>
int main(){
    int subject1 = 80;
    int subject2 = 90;
    float average = (subject1 + subject2) / 2.0;
    printf("Average: %.2f", average);
    return 0;
}
C++
#include <iostream>
using namespace std;
int main(){
    int subject1 = 80;
    int subject2 = 90;
    float average = (subject1 + subject2) / 2.0;
    cout << "Average: " << average;
    return 0;
}
Java
public class Main{
    public static void main(String[] args){
        int subject1 = 80;
        int subject2 = 90;
        double average = (subject1 + subject2) / 2.0;
        System.out.println("Average: " + average);
    }
}
Python
subject1 = 80
subject2 = 90
average = (subject1 + subject2) / 2
print("Average:", average)
Expected Output: Average: 85.0

2.6 Strings and Boolean

A string stores text. A Boolean stores true or false.

Working Example: Name and Active Status

C
#include <stdio.h>
#include <stdbool.h>
int main(){
    char name[] = "PDTC";
    bool active = true;
    printf("Name: %s\n", name);
    printf("Active: %d", active);
    return 0;
}
C++
#include <iostream>
#include <string>
using namespace std;
int main(){
    string name = "PDTC";
    bool active = true;
    cout << "Name: " << name << endl;
    cout << "Active: " << active;
    return 0;
}
Java
public class Main{
    public static void main(String[] args){
        String name = "PDTC";
        boolean active = true;
        System.out.println("Name: " + name);
        System.out.println("Active: " + active);
    }
}
Python
name = "PDTC"
active = True
print("Name:", name)
print("Active:", active)

2.7 Type Conversion

Type conversion changes data from one type to another.

Working Example: Convert String to Number

C
#include <stdio.h>
#include <stdlib.h>
int main(){
    char text[] = "100";
    int number = atoi(text);
    printf("Number + 50 = %d", number + 50);
    return 0;
}
C++
#include <iostream>
#include <string>
using namespace std;
int main(){
    string text = "100";
    int number = stoi(text);
    cout << "Number + 50 = " << number + 50;
    return 0;
}
Java
public class Main{
    public static void main(String[] args){
        String text = "100";
        int number = Integer.parseInt(text);
        System.out.println("Number + 50 = " + (number + 50));
    }
}
Python
text = "100"
number = int(text)
print("Number + 50 =", number + 50)
Expected Output: Number + 50 = 150

2.8 Arithmetic Operators

Arithmetic operators perform mathematical calculations.

OperatorMeaning
+Addition
-Subtraction
*Multiplication
/Division
%Modulus / Remainder

Working Example: Calculator Operations

C
#include <stdio.h>
int main(){
    int a = 10, b = 3;
    printf("Add: %d\n", a + b);
    printf("Subtract: %d\n", a - b);
    printf("Multiply: %d\n", a * b);
    printf("Divide: %.2f\n", a / (float)b);
    printf("Remainder: %d", a % b);
    return 0;
}
C++
#include <iostream>
using namespace std;
int main(){
    int a = 10, b = 3;
    cout << "Add: " << a + b << endl;
    cout << "Subtract: " << a - b << endl;
    cout << "Multiply: " << a * b << endl;
    cout << "Divide: " << (float)a / b << endl;
    cout << "Remainder: " << a % b;
    return 0;
}
Java
public class Main{
    public static void main(String[] args){
        int a = 10, b = 3;
        System.out.println("Add: " + (a + b));
        System.out.println("Subtract: " + (a - b));
        System.out.println("Multiply: " + (a * b));
        System.out.println("Divide: " + ((double)a / b));
        System.out.println("Remainder: " + (a % b));
    }
}
Python
a = 10
b = 3
print("Add:", a + b)
print("Subtract:", a - b)
print("Multiply:", a * b)
print("Divide:", a / b)
print("Remainder:", a % b)

2.9 Comparison Operators

Comparison operators compare two values and return true or false.

OperatorMeaning
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Working Example: Passing Marks

C
#include <stdio.h>
int main(){
    int marks = 65;
    printf("Pass Status: %d", marks >= 50);
    return 0;
}
C++
#include <iostream>
using namespace std;
int main(){
    int marks = 65;
    cout << "Pass Status: " << (marks >= 50);
    return 0;
}
Java
public class Main{
    public static void main(String[] args){
        int marks = 65;
        System.out.println("Pass Status: " + (marks >= 50));
    }
}
Python
marks = 65
print("Pass Status:", marks >= 50)

2.10 Logical Operators

Logical operators combine multiple conditions.

LogicC / C++ / JavaPython
AND&&and
OR||or
NOT!not

Working Example: Scholarship Eligibility

C
#include <stdio.h>
int main(){
    int marks = 85;
    int attendance = 92;
    printf("Eligible: %d", marks >= 80 && attendance >= 90);
    return 0;
}
C++
#include <iostream>
using namespace std;
int main(){
    int marks = 85;
    int attendance = 92;
    cout << "Eligible: " << (marks >= 80 && attendance >= 90);
    return 0;
}
Java
public class Main{
    public static void main(String[] args){
        int marks = 85;
        int attendance = 92;
        System.out.println("Eligible: " + (marks >= 80 && attendance >= 90));
    }
}
Python
marks = 85
attendance = 92
print("Eligible:", marks >= 80 and attendance >= 90)

2.11 Operator Precedence

Operator precedence controls which operation is performed first.

C
#include <stdio.h>
int main(){
    int result = 5 + 2 * 3;
    printf("%d", result);
    return 0;
}
C++
#include <iostream>
using namespace std;
int main(){
    int result = 5 + 2 * 3;
    cout << result;
    return 0;
}
Java
public class Main{
    public static void main(String[] args){
        int result = 5 + 2 * 3;
        System.out.println(result);
    }
}
Python
result = 5 + 2 * 3
print(result)
Expected Output: 11

2.12 Combined Practical Example: Student Result System

This example uses variables, numeric data types, arithmetic, comparison and logical operators.

C
#include <stdio.h>
int main(){
    int test1 = 80, test2 = 70, test3 = 90;
    float average = (test1 + test2 + test3) / 3.0;
    printf("Average: %.2f\n", average);
    printf("Pass: %d", average >= 50);
    return 0;
}
C++
#include <iostream>
using namespace std;
int main(){
    int test1 = 80, test2 = 70, test3 = 90;
    float average = (test1 + test2 + test3) / 3.0;
    cout << "Average: " << average << endl;
    cout << "Pass: " << (average >= 50);
    return 0;
}
Java
public class Main{
    public static void main(String[] args){
        int test1 = 80, test2 = 70, test3 = 90;
        double average = (test1 + test2 + test3) / 3.0;
        System.out.println("Average: " + average);
        System.out.println("Pass: " + (average >= 50));
    }
}
Python
test1 = 80
test2 = 70
test3 = 90
average = (test1 + test2 + test3) / 3
print("Average:", average)
print("Pass:", average >= 50)

2.13 Interactive Calculator Demo

This browser demo demonstrates arithmetic operators.

Click Calculate.

2.14 Practical Activities

Activity 1: Variable Declaration

Declare name, age and marks in C, C++, Java and Python.

Activity 2: Constants

Use a constant tax rate and calculate final price in all four languages.

Activity 3: Type Conversion

Convert text value "250" into number and add 50 in each language.

Activity 4: Logical Operators

Check scholarship eligibility using marks and attendance.

Mini Project

Create a student grading system in C, C++, Java and Python using variables, operators and comparisons.

2.15 Interactive Final Assessment Quiz

Each correct answer gives +1 mark. Each wrong answer gives -0.5 mark.

1. A variable stores data in a program.

2. Which keyword is used for constants in Java?

3. Python requires explicit data type declaration for every variable.

4. Which operator gives remainder?

5. In C, C++ and Java, AND is written as &&.

6. In Python, Boolean true is written as True.

7. Which Python function converts text to integer?

8. The expression 5 + 2 * 3 gives 11.

9. Comparison operators return true or false.

10. String values normally store text.

Your Score: 0

2.16 Module Summary

This module explained variables, constants, numeric data types, strings, Boolean values, type conversion, arithmetic operators, comparison operators and logical operators using C, C++, Java and Python as a comparative study.

Remember: The syntax may change from one language to another, but the programming logic remains very similar.