C++ Tutorial
C++ Syntax
Statements end with a semicolon. Blocks use braces. The compiler cares about types, not indentation.
A program is statements in main
C++ reads a file from top to bottom. Directives such as #include come first. Then you declare functions. Execution starts at main. Inside main, each statement ends with a semicolon.
Example
#include <iostream>
using namespace std;
int main() {
int n = 3;
cout << n << endl;
return 0;
}Semicolons
Forget a semicolon and g++ will complain, often on the next line. Python uses newlines. C++ uses; so you can put more than one statement on a line — you usually should not.
Example
int a = 1;
int b = 2;
cout << a + b << endl;Braces mark blocks
{ } groups statements: the body of main, an if, a loop, a function. Indentation is for people. The compiler only sees braces. Mismatched braces are a common first-week error.
Example
if (true) {
cout << "inside the block" << endl;
}Case and names
C++ is case-sensitive. Main is not main. Cout is not cout. Names start with a letter or underscore, then letters, digits, or underscores. Do not start a name with a digit.
| Valid | Invalid |
|---|---|
| count, player2, _tmp | 2player, my-score, class |
class is a keyword. You cannot use it as a variable name. The compiler will say so.
Whitespace
Spaces and blank lines do not change meaning. Use them so a human can scan the file. One statement per line is the house style on StudyGrid.
#include is a preprocessor directive. It is not a C++ statement and does not take a semicolon.
main returns int
Write int main() and return 0; at the end. Other signatures exist; this tutorial sticks to the portable form. Next: how cout prints values.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C++ editor at /cpp/try. Change one measurement and check whether the result still has the right unit.
Maths
Area of a rectangle
Area is length times width. For a rectangle 4 m by 3 m that is 12 m². Each step is one statement. The semicolon is not decoration; without it g++ stops.
Braces around main are the block that owns those statements. Indentation is for you. The compiler does not care how many spaces you use.
A = w × h
Example
#include <iostream>
using namespace std;
int main() {
double width = 4.0;
double height = 3.0;
double area = width * height;
cout << "area = " << area << " m^2" << endl;
return 0;
}Physics
Minutes into hours
Time in kinematics is usually in seconds, but a stopwatch in a sports hall is often in minutes. 150 minutes is 2.5 hours because there are 60 minutes in an hour.
Divide by 60.0, not 60, if you want a fractional hour. Integer 150 / 60 is 2, and the leftover 30 minutes vanish.
hours = minutes / 60
Example
#include <iostream>
using namespace std;
int main() {
int minutes = 150;
double hours = minutes / 60.0;
cout << minutes << " min = " << hours << " h" << endl;
return 0;
}