Logo/
LogoDetailed Notes — C++ Variables, Data Types & Operators

Made with ScholarlyTurn your own files into documents like this

Shared with you

Detailed Notes — C++ Variables, Data Types & Operators

C++ Variables, Data Types & Operators

Lecture overview

This lecture builds a foundation for writing small C++ programs. The recurring program pattern is:

Input → Processing → Output

You learn how to print text, compile and run a program, store information in variables, work with primitive data types, convert between types, accept keyboard input, and use arithmetic, relational, logical, increment, and decrement operators.

Source note: The available transcript extraction is partial for some middle sections. The notes preserve the lecture’s identifiable explanations and examples; the relational/logical operator examples are included as concept notes where the extracted transcript did not contain the full explanation.
  1. Printing your first output

Timestamp: 0:46–1:46

C++ uses cout to display output. The insertion operator << sends text or values to the output stream.

cout << "Hello World";

Important syntax rules:

  • Text must be placed inside double quotation marks: "Hello World".
  • C++ is case-sensitive: cout and Cout are different.
  • Most statements end with a semicolon ;.
  • cout is supplied by the input/output library.

cout can also print numbers and variables:

cout << 25;
cout << age;

Key idea: cout prints whatever expression is placed after <<.

  1. Setting up a C++ file in VS Code

Timestamp: 2:54–4:48

The basic workflow is:

  1. Open or create a project folder in VS Code.
  2. Open the Explorer panel.
  3. Create a file whose name ends in .cpp, such as code.cpp.
  4. Write the program and save it.
  5. Use the integrated terminal to compile and run it.

The .cpp extension tells you that the file contains C++ source code.

  1. C++ boilerplate and the main() function

Timestamp: 5:28–11:30

A minimal C++ program is:

#include <iostream>
using namespace std;

int main() {
    cout << "Hello World";
    return 0;
}

What each line does

  • #include <iostream> includes the input/output library, which provides tools such as cout and cin.
  • using namespace std; allows you to write cout and cin instead of std::cout and std::cin.
  • int main() defines the main function.
  • main() is the starting point of program execution.
  • Curly braces {} contain the function body.
  • return 0; ends main() and returns a successful status to the operating system.

An alternative that avoids using namespace std; is:

#include <iostream>

int main() {
    std::cout << "Hello World";
    return 0;
}

Exam takeaway: Every executable C++ program needs a starting point, and that starting point is main().

  1. Compiling and running a program

Timestamp: 11:48–15:06

C++ source code must be compiled before it can run. Compilation translates the source code into an executable program.

Save the file first, then compile it in the terminal:

g++ code.cpp

On macOS or Linux, run the default executable with:

./a.out

A combined command is:

g++ code.cpp && ./a.out

The && means the second command runs only if compilation succeeds.

Useful terminal actions:

  • Ctrl+S or Cmd+S: save the file.
  • clear: clear the terminal display.
  • Up-arrow: recall a previous terminal command.

On Windows, the output executable may have an .exe extension, depending on the compiler setup.

Debugging habit: If the program does not run, first check whether compilation produced an error. Fix compilation errors before trying to execute the program.

  1. Printing multiple lines and formatting output

Timestamp: 15:29–19:23

endl moves the cursor to the next line:

cout << "First line" << endl;
cout << "Second line" << endl;

The newline escape sequence \n also starts a new line:

cout << "First line\nSecond line";

You can chain several pieces of output in one statement:

cout << "My College " << "Hello World" << endl;

endl versus \n

  • endl inserts a newline and flushes the output buffer.
  • \n inserts a newline without explicitly flushing the buffer.
  • Both produce a visible line break. \n is often preferred when frequent output performance matters.
  1. Comments

Timestamp: 19:48–21:39

Comments explain code to humans and are ignored by the compiler.

A single-line comment begins with //:

// Program starting point
int main() {
    cout << "Hello World";
}

Comments are useful for:

  • Explaining what a section does.
  • Leaving reminders.
  • Making unfamiliar code easier to understand.
  • Temporarily disabling one line during debugging.

In VS Code, Ctrl+/ on Windows/Linux or Cmd+/ on Mac toggles a line comment.

  1. Variables and memory

Timestamp: 22:03 onward; variable examples around 32:42–42:43

A variable is a named memory location used to store a value. The declaration tells C++ what kind of value the variable will hold.

int age = 20;
double price = 100.9;
char grade = 'A';
bool isSafe = true;

The assignment operator = places the value on the right into the variable on the left:

int number = 25;

Here, 25 is stored in the memory location represented by number.

Computers ultimately represent data using binary digits: 0 and 1. The data type helps the compiler decide how to interpret and store those bits.

Character storage and ASCII

Characters are represented internally by numeric codes. In ASCII:

  • 'A' corresponds to 65.
  • 'B' corresponds to 66.
  • 'a' corresponds to 97.
  • 'b' corresponds to 98.

For example:

char grade = 'A';
cout << grade; // A
cout << (int)grade; // 65

The quotes matter: 'A' is a character literal, while "A" is a string literal.

  1. Primitive data types

Timestamp: 26:24–37:45

Primitive data types are the basic built-in types used to represent common values.

int

int count = 100;

Use int for whole-number values. It does not store a fractional part.

float

float pi = 3.14f;

The f suffix tells the compiler that the literal should be treated as a float. Without the suffix, a decimal literal such as 3.14 is generally treated as a double.

double

double price = 100.9;

double generally provides more precision and uses more memory than float.

char

char section = 'A';

A char stores one character and internally corresponds to a numeric character code.

bool

bool isLoggedIn = true;

A Boolean has two logical values:

true  // commonly represented as 1
false // commonly represented as 0

When printed directly, Boolean values commonly appear as 1 and 0 unless Boolean formatting is changed.

  1. Type conversion and type casting

Timestamp: 37:57–42:43

Implicit type conversion

Type conversion happens automatically when the compiler changes one compatible type into another.

float value = 3.14f;
double largerValue = value;

A float can be converted to a double because double generally provides more storage and precision.

A char can also be converted to an int:

char grade = 'A';
int numericValue = grade;
cout << numericValue; // 65

Explicit type casting

Type casting is a conversion deliberately requested by the programmer.

double price = 1.99;
int newPrice = (int)price;
cout << newPrice; // 1

Casting from double to int removes the decimal portion; it does not round to the nearest integer.

(int)100.99  // 100
(int)100.01  // 100

Modern C++ also supports a clearer cast syntax:

int newPrice = static_cast<int>(price);

Important: Converting from a type with more information to one with less information can cause data loss.

  1. Taking input with cin

Timestamp: 42:47–47:06

cin reads input from the keyboard. The extraction operator >> places the entered value into a variable.

#include <iostream>
using namespace std;

int main() {
    int age;
    cin >> age;
    cout << "Age = " << age << endl;
    return 0;
}

A program commonly follows this sequence:

  1. Declare variables.
  2. Read input.
  3. Process the values.
  4. Print the result.

Multiple values can be read in one statement:

int a, b;
cin >> a >> b;

If the user enters 3 10, then a becomes 3 and b becomes 10.

  1. Arithmetic operators

Timestamp: 47:06 onward

Arithmetic operators perform numerical calculations.

int a = 10, b = 3;

cout << a + b << endl;
cout << a - b << endl;
cout << a * b << endl;
cout << a / b << endl;
cout << a % b << endl;

Integer division

When both operands are integers, division discards the fractional part:

cout << 10 / 3; // 3, not 3.333...

To preserve the decimal result, use a decimal operand:

cout << 10.0 / 3; // approximately 3.33333

Modulus

The modulus operator returns the remainder:

10 % 3 // 1
12 % 4 // 0

A remainder of 0 means the first number is divisible by the second number.

  1. Relational operators

Timestamp: approximately 55:32 onward

Relational operators compare two values. The result is Boolean: true or false, commonly printed as 1 or 0.

int a = 10, b = 3;

cout << (a > b) << endl;  // 1
cout << (a < b) << endl;  // 0
cout << (a == b) << endl; // 0
cout << (a != b) << endl; // 1

Common mistake

  • = assigns a value.
  • == compares two values.
int x = 5;      // assignment
x == 5;         // comparison
  1. Logical operators

Timestamp: approximately 55:32 onward

Logical operators combine or modify conditions.

Example:

int a = 10, b = 3;

cout << (a > 0 && b > 0) << endl; // 1
cout << (a > 0 || b < 0) << endl; // 1
cout << !(a == b) << endl;        // 1

Use parentheses to make compound conditions clear and avoid ambiguity.

  1. Building a sum program

Timestamp: 64:27–66:19

The lecture applies the input–processing–output pattern to adding two numbers.

#include <iostream>
using namespace std;

int main() {
    int a, b;

    // Input
    cin >> a >> b;

    // Processing
    int sum = a + b;

    // Output
    cout << "sum = " << sum << endl;

    return 0;
}

For input 3 10, the output is:

sum = 13

The important structure is more general than addition:

read values → calculate something → display the result
  1. Unary increment and decrement operators

Timestamp: 66:39–74:51

A binary operator works with two operands, such as a + b. A unary operator works with one operand.

Increment

++ increases a value by 1.

int a = 10;
a++;
// a is now 11

Decrement

-- decreases a value by 1.

int a = 10;
a--;
// a is now 9

Post-increment versus pre-increment

The position of the operator matters when the expression both uses and changes the value.

int a = 10;
int b = a++;

After this:

b = 10
 a = 11

a++ means: use the old value first, then increment a.

int a = 10;
int b = ++a;

After this:

 a = 11
b = 11

++a means: increment a first, then use the new value.

The same distinction applies to a-- and --a.

Memory aid:

  • Postfix: use, then change.
  • Prefix: change, then use.
  1. Final recap and homework

Timestamp: 74:37–76:10

The lecture’s main ideas are:

  • cout prints output.
  • cin reads input.
  • main() is the starting point of execution.
  • Variables store values in memory.
  • Data types determine what kind of value a variable stores.
  • Type conversion can happen automatically.
  • Type casting is requested explicitly by the programmer.
  • Arithmetic operators calculate values.
  • Relational operators compare values.
  • Logical operators combine conditions.
  • ++ and -- change a value by one.
  • Most beginner programs can be understood as input → processing → output.

Homework: build a calculator

Create a program that accepts two numbers and an operator, then performs:

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Modulus: %

A simple starting version could be:

#include <iostream>
using namespace std;

int main() {
    int a, b;
    char operation;

    cin >> a >> operation >> b;

    if (operation == '+') {
        cout << a + b;
    } else if (operation == '-') {
        cout << a - b;
    } else if (operation == '*') {
        cout << a * b;
    } else if (operation == '/') {
        cout << a / b;
    } else if (operation == '%') {
        cout << a % b;
    }

    return 0;
}

Before testing division or modulus, make sure the second number is not zero.

Quick self-test

  1. What is the purpose of main()?
  2. What is the difference between cout and cin?
  3. Why is 3.14f written with an f?
  4. What is the ASCII value of 'A'?
  5. What is the result of 10 / 3 when both values are integers?
  6. What is the result of 10 % 3?
  7. What is the difference between = and ==?
  8. What is the difference between a++ and ++a?
  9. What are the three stages of a basic program?
  10. Why can casting a double to an int lose information?

Answers

  1. It is the starting point of program execution.
  2. cout outputs data; cin accepts input.
  3. It identifies the decimal literal as a float.
  4. 65.
  5. 3.
  6. 1.
  7. = assigns; == compares.
  8. a++ uses the old value before incrementing; ++a increments before using the value.
  9. Input, processing, output.
  10. The fractional part is discarded.

Ask AI about this page

Answers cite the source. Free account, no credit card.