HSSLIVE Plus One Computer Science Chapter 6: Data Types and Operators Notes

This chapter explores how C++ implements various data types to represent information, including integers, floating-point numbers, characters, and Boolean values. It examines the properties and limitations of each data type, including memory allocation and range of representable values. The chapter also covers the rich set of operators available in C++—arithmetic, relational, logical, bitwise, and assignment operators—explaining operator precedence and demonstrating how expressions are evaluated to produce results that drive program behavior.

Chapter 6: Data Types and Operators

Introduction to Data Types

In computer programming, data types are classifications that specify what kind of data a variable can hold. Understanding data types is fundamental because they determine:

  • How much memory is allocated
  • What operations can be performed
  • How the data is stored and interpreted

Think of data types like different kinds of containers – some are designed to hold numbers, others text, and others more complex information.

Basic Data Types

Integer (int)

Integers are whole numbers without any decimal part. They can be positive, negative, or zero.

Examples: -10, 0, 42, 1000

In most programming languages, integers have a limited range because they’re stored in a fixed amount of memory. For example, a standard integer might use 4 bytes of memory, allowing values from approximately -2 billion to +2 billion.

Floating-Point (float, double)

Floating-point types store numbers with decimal points. They come in two common forms:

  • float: Single precision (typically 4 bytes)
  • double: Double precision (typically 8 bytes) with greater accuracy

Examples: 3.14159, -0.5, 2.0, 1.38e-23

Floating-point numbers can represent a much wider range of values than integers but may have precision issues with certain calculations.

Character (char)

The character data type stores a single character, which could be a letter, digit, punctuation mark, or special symbol.

Examples: ‘A’, ‘7’, ‘$’, ‘+’

Characters are typically stored using ASCII or Unicode encoding systems, which assign a unique number to each character.

Boolean (bool)

Boolean data types can have only two values: true or false. They’re used for logical operations and decision-making in programs.

Examples: true, false

Although conceptually simple, booleans are powerful for controlling program flow through conditional statements.

String

A string is a sequence of characters used to represent text. In many languages, strings are enclosed in quotation marks.

Examples: “Hello, World!”, “Kerala”, “12345”

Strings can contain letters, numbers, spaces, and special characters. Their length can vary from empty (“”) to thousands of characters.

Derived Data Types

Arrays

An array is a collection of elements of the same data type, stored in contiguous memory locations. Each element can be accessed using an index.

Example: An array of integers [10, 20, 30, 40, 50]

Arrays provide an efficient way to store and process multiple related values.

Structures (struct)

Structures group related data of different types under a single name. Each piece of data in a structure is called a member or field.

Example: A student structure might contain name (string), roll number (integer), and grade (character).

Structures help organize related data logically.

Pointers

Pointers store memory addresses of other variables. They “point to” the location of data rather than storing the data itself.

Example: If a variable ‘x’ is stored at memory address 0x7fff5694, a pointer to ‘x’ would contain this address.

Pointers enable dynamic memory allocation and efficient handling of large data structures.

Type Conversion

Type conversion is the process of converting data from one type to another.

Implicit Conversion (Automatic)

Happens automatically when the compiler converts one data type to another without programmer intervention.

Example: When adding an integer and a float, the integer is automatically converted to float before the operation.

int a = 5;
float b = 2.5;
float result = a + b;  // 'a' is implicitly converted to float

Explicit Conversion (Type Casting)

Programmer-directed conversion of data from one type to another.

Example:

float x = 9.7;
int y = (int)x;  // Explicitly converting float to int, resulting in y = 9

Variables and Constants

Variables

A variable is a named storage location in memory whose value can change during program execution.

Syntax for variable declaration:

data_type variable_name;

Example:

int age;
float height;
char grade;

Constants

Constants are values that cannot be modified once defined. They are used when you need a fixed value throughout your program.

Example:

const float PI = 3.14159;
const int MAX_STUDENTS = 50;

Operators

Operators are symbols that tell the computer to perform specific operations.

Arithmetic Operators

Used for mathematical calculations:

  • Addition (+)a + b
  • Subtraction (-)a - b
  • Multiplication (*)a * b
  • Division (/)a / b
  • Modulus (%)a % b (returns the remainder after division)
  • Increment (++)a++ (increases value by 1)
  • Decrement (–)a-- (decreases value by 1)

Example:

int a = 10, b = 3;
int sum = a + b;      // 13
int diff = a - b;     // 7
int product = a * b;  // 30
int quotient = a / b; // 3 (integer division)
int remainder = a % b; // 1

Relational Operators

Compare values and return a boolean result:

  • Equal to (==)a == b
  • Not equal to (!=)a != b
  • Greater than (>)a > b
  • Less than (<)a < b
  • Greater than or equal to (>=)a >= b
  • Less than or equal to (<=)a <= b

Example:

int x = 5, y = 8;
bool isEqual = (x == y);     // false
bool isGreater = (x > y);    // false
bool isLessEqual = (x <= y); // true

Logical Operators

Combine or modify boolean expressions:

  • AND (&&): True if both conditions are true
  • OR (||): True if at least one condition is true
  • NOT (!): Inverts a boolean value

Example:

bool hasPass = true;
bool hasID = false;
bool canEnter = hasPass && hasID; // false (both must be true)
bool canExit = hasPass || hasID;  // true (at least one is true)
bool denyEntry = !canEnter;       // true (inverts false to true)

Assignment Operators

Used to assign values to variables:

  • Simple assignment (=)a = b
  • Add and assign (+=)a += b (equivalent to a = a + b)
  • Subtract and assign (-=)a -= b
  • Multiply and assign (*=)a *= b
  • Divide and assign (/=)a /= b
  • Modulus and assign (%=)a %= b

Example:

int x = 10;
x += 5;  // x becomes 15
x -= 3;  // x becomes 12
x *= 2;  // x becomes 24

Bitwise Operators

Perform operations on individual bits of binary numbers:

  • AND (&): Sets each bit to 1 if both bits are 1
  • OR (|): Sets each bit to 1 if at least one bit is 1
  • XOR (^): Sets each bit to 1 if only one bit is 1
  • NOT (~): Inverts all bits
  • Left shift (<<): Shifts bits left, filling with zeros
  • Right shift (>>): Shifts bits right

Example:

int a = 5;  // 101 in binary
int b = 3;  // 011 in binary
int c = a & b;  // 001 (1 in decimal)
int d = a | b;  // 111 (7 in decimal)

Operator Precedence

Not all operators are evaluated equally. Like in mathematics, computer languages follow a precedence order:

  1. Parentheses ()
  2. Unary operators (++, –, !)
  3. Multiplication, Division, Modulus (*, /, %)
  4. Addition, Subtraction (+, -)
  5. Relational operators (<, >, <=, >=)
  6. Equality operators (==, !=)
  7. Logical AND (&&)
  8. Logical OR (||)
  9. Assignment operators (=, +=, -=, etc.)

Example:

int result = 5 + 3 * 2;     // 11 (not 16) because * has higher precedence than +
int result2 = (5 + 3) * 2;  // 16 because parentheses have highest precedence

Expressions and Statements

Expressions

Combinations of values, variables, and operators that evaluate to a single value.

Examples:

  • 5 + 3 (evaluates to 8)
  • x * y (evaluates to the product of x and y)
  • a > b (evaluates to a boolean value)

Statements

Complete instructions that perform actions. Statements typically end with a semicolon in many languages.

Examples:

  • int score = 95; (declaration statement)
  • total = price + tax; (assignment statement)
  • if (age >= 18) { /* code */ } (control statement)

Understanding data types and operators provides the foundation for writing effective programs. They are the building blocks with which you’ll construct more complex logic and functionality as you continue learning programming.

Leave a Comment