Made with ScholarlyTurn your own files into documents like this
Shared with you
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.
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:
cout can also print numbers and variables:
cout << 25; cout << age;
Key idea: cout prints whatever expression is placed after <<.
Timestamp: 2:54–4:48
The basic workflow is:
The .cpp extension tells you that the file contains C++ source code.
Timestamp: 5:28–11:30
A minimal C++ program is:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World";
return 0;
}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().
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:
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.
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;
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:
In VS Code, Ctrl+/ on Windows/Linux or Cmd+/ on Mac toggles a line comment.
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.
Characters are represented internally by numeric codes. In ASCII:
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.
Timestamp: 26:24–37:45
Primitive data types are the basic built-in types used to represent common values.
int count = 100;
Use int for whole-number values. It does not store a fractional part.
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 price = 100.9;
double generally provides more precision and uses more memory than float.
char section = 'A';
A char stores one character and internally corresponds to a numeric character code.
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.
Timestamp: 37:57–42:43
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
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.
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:
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.
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;
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
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.
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
int x = 5; // assignment x == 5; // comparison
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.
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
Timestamp: 66:39–74:51
A binary operator works with two operands, such as a + b. A unary operator works with one operand.
++ increases a value by 1.
int a = 10; a++; // a is now 11
-- decreases a value by 1.
int a = 10; a--; // a is now 9
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:
Timestamp: 74:37–76:10
The lecture’s main ideas are:
Create a program that accepts two numbers and an operator, then performs:
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.