Made with ScholarlyTurn your own files into documents like this
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.
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.
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:
Key idea: cout prints whatever expression is placed after <<.
Setting up a C++ file in VS Code
Timestamp: 2:54–4:48
The basic workflow is:
Open or create a project folder in VS Code.
Open the Explorer panel.
Create a file whose name ends in .cpp, such as code.cpp.
Write the program and save it.
Use the integrated terminal to compile and run it.
The .cpp extension tells you that the file contains C++ source code.
C++ boilerplate and the main() function
Timestamp: 5:28–11:30
A minimal C++ program is:
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:
Exam takeaway: Every executable C++ program needs a starting point, and that starting point is main().
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:
On macOS or Linux, run the default executable with:
A combined command is:
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.
Printing multiple lines and formatting output
Timestamp: 15:29–19:23
endl moves the cursor to the next line:
The newline escape sequence \n also starts a new line:
You can chain several pieces of output in one statement:
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.
Comments
Timestamp: 19:48–21:39
Comments explain code to humans and are ignored by the compiler.
A single-line comment begins with //:
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.
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.
The assignment operator = places the value on the right into the variable on the left:
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:
The quotes matter: 'A' is a character literal, while "A" is a string literal.
Primitive data types
Timestamp: 26:24–37:45
Primitive data types are the basic built-in types used to represent common values.
int
Use int for whole-number values. It does not store a fractional part.
float
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 generally provides more precision and uses more memory than float.
char
A char stores one character and internally corresponds to a numeric character code.
bool
A Boolean has two logical values:
When printed directly, Boolean values commonly appear as 1 and 0 unless Boolean formatting is changed.
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.
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:
Explicit type casting
Type casting is a conversion deliberately requested by the programmer.
Casting from double to int removes the decimal portion; it does not round to the nearest integer.
Modern C++ also supports a clearer cast syntax:
Important: Converting from a type with more information to one with less information can cause data loss.
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.
A program commonly follows this sequence:
Declare variables.
Read input.
Process the values.
Print the result.
Multiple values can be read in one statement:
If the user enters 3 10, then a becomes 3 and b becomes 10.
Arithmetic operators
Timestamp: 47:06 onward
Arithmetic operators perform numerical calculations.
Integer division
When both operands are integers, division discards the fractional part:
To preserve the decimal result, use a decimal operand:
Modulus
The modulus operator returns the remainder:
A remainder of 0 means the first number is divisible by the second number.
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.
Common mistake
= assigns a value.
== compares two values.
Logical operators
Timestamp: approximately 55:32 onward
Logical operators combine or modify conditions.
Example:
Use parentheses to make compound conditions clear and avoid ambiguity.
Building a sum program
Timestamp: 64:27–66:19
The lecture applies the input–processing–output pattern to adding two numbers.
For input 3 10, the output is:
The important structure is more general than addition:
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.
Decrement
-- decreases a value by 1.
Post-increment versus pre-increment
The position of the operator matters when the expression both uses and changes the value.
After this:
a++ means: use the old value first, then increment a.
After this:
++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.
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:
Before testing division or modulus, make sure the second number is not zero.
Quick self-test
What is the purpose of main()?
What is the difference between cout and cin?
Why is 3.14f written with an f?
What is the ASCII value of 'A'?
What is the result of 10 / 3 when both values are integers?
What is the result of 10 % 3?
What is the difference between = and ==?
What is the difference between a++ and ++a?
What are the three stages of a basic program?
Why can casting a double to an int lose information?
Answers
It is the starting point of program execution.
cout outputs data; cin accepts input.
It identifies the decimal literal as a float.
65.
3.
1.
= assigns; == compares.
a++ uses the old value before incrementing; ++a increments before using the value.
Input, processing, output.
The fractional part is discarded.