Introduction to C++
Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.
Why C++ Now
Welcome to C++ for Finance
You already know how to think like a programmer from JavaScript: values, functions, conditions, loops, and debugging are familiar territory.
C++ asks you to become more explicit. You tell the compiler what kind of data you are storing, how functions should be called, and eventually how memory is managed.
That explicitness is one reason C++ still matters in finance: low-latency trading systems, pricing engines, risk platforms, and data infrastructure often care about speed, predictability, and control.
In this first lesson, we will build the foundation:
- variables and data types
- operators
- control structures
- functions
- the compile-and-run workflow
Later lessons will connect these basics to finance-specific workflows, trading logic, risk, and data analysis.

Compiled Mindset
C++ is compiled
In JavaScript, you often write code and run it immediately in Node or the browser.
C++ usually has an extra step: a compiler translates your source code into a machine-executable program.
main.cpp -> compiler -> executable -> output
That compiler is strict. It catches many mistakes before the program runs, especially type-related mistakes.
This can feel slower at first, but it is like learning proper lifting form: more deliberate reps now create safer, stronger performance later.
A typical local command looks like:
g++ main.cpp -o main
./main
You do not need to master build systems yet. For now, focus on writing small programs that compile cleanly.

Variables And Types
Values need declared types
In JavaScript, you might write:
let price = 125.50;
In C++, you usually declare the type up front:
-
intfor whole numbers -
doublefor decimal numbers -
charfor a single character -
boolfor true/false -
std::stringfor text
For finance-adjacent code, double is common for learning examples involving prices or percentages. Real production systems may use stricter representations, but that comes later.
Here is a tiny complete C++ program.
```cpp
#include <iostream>
#include <string>
int main() {
std::string ticker = "ABC";
double price = 125.50;
int shares = 10;
bool marketOpen = true;
std::cout << "Ticker: " << ticker << std::endl;
std::cout << "Price: " << price << std::endl;
std::cout << "Shares: " << shares << std::endl;
std::cout << "Market open? " << marketOpen << std::endl;
return 0;
}
```
Type Check
Which C++ type is most appropriate for storing a simple learning-example stock price like 101.75?
Operators Build Expressions
Operators combine values
C++ uses many operators you already know from JavaScript:
-
+addition -
-subtraction -
*multiplication -
/division -
%remainder for integers -
==,!=,<,>comparisons -
&&,||,!logical operators
One important habit: think about the type of each expression.
For example, shares * price combines an int and a double, and the result is a double because decimals are involved.
```cpp
#include <iostream>
int main() {
int shares = 12;
double buyPrice = 80.00;
double currentPrice = 84.50;
double cost = shares * buyPrice;
double value = shares * currentPrice;
double profit = value - cost;
bool isProfitable = profit > 0;
std::cout << "Cost: " << cost << std::endl;
std::cout << "Current value: " << value << std::endl;
std::cout << "Profit: " << profit << std::endl;
std::cout << "Profitable? " << isProfitable << std::endl;
return 0;
}
```
No Hoisting
True or false: In C++, you can use a local variable before declaring it, similar to relying on JavaScript hoisting.
Control Flow
Programs make decisions
Control flow lets your program choose what to do next.
The core tools are:
-
if,else if,elsefor branching -
forloops when you know the number of repetitions -
whileloops when you repeat until a condition changes
This is familiar from JavaScript, but C++ syntax is stricter about types and declarations.
A basketball analogy: you do not run every play the same way. You read the scoreboard, possession, and clock, then choose the right action.
```cpp
#include <iostream>
int main() {
double dailyReturns[] = {0.01, -0.02, 0.015, 0.005};
int positiveDays = 0;
for (double r : dailyReturns) {
if (r > 0) {
positiveDays = positiveDays + 1;
}
}
if (positiveDays >= 3) {
std::cout << "Strong short stretch" << std::endl;
} else {
std::cout << "Mixed short stretch" << std::endl;
}
std::cout << "Positive days: " << positiveDays << std::endl;
return 0;
}
```
Choose The Type
Fill in the blank with the C++ type commonly used in beginner examples for decimal values:
_____ price = 99.95;
Functions Package Logic
Functions make code reusable
A function packages a task behind a name.
In C++, a function signature usually tells you three things:
return_type function_name(parameter_types)
That means the compiler knows:
- what the function gives back
- what inputs it expects
- whether your call is valid
This explicit structure is useful as programs grow. Instead of repeating calculations everywhere, you define them once and call them when needed.
```cpp
#include <iostream>
double portfolioValue(int shares, double price) {
return shares * price;
}
int main() {
int shares = 25;
double price = 42.20;
double value = portfolioValue(shares, price);
std::cout << "Portfolio value: " << value << std::endl;
return 0;
}
```
Your C++ Foundation
What you now have
You have seen the core shape of a C++ program:
- include libraries with
#include - start execution in
main() - declare typed variables
- build expressions with operators
- branch and loop with control flow
- package reusable behavior with functions
- print output with
std::cout
The big shift from JavaScript is not that programming is different. It is that C++ asks you to be more precise earlier.
That precision is exactly what we will build on next in C++ for Finance Basics, where these language fundamentals start becoming small finance-oriented programs.
