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.

ValidInvalid
count, player2, _tmp2player, 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;
}

FAQ: C++ Syntax

Common questions about this page.

What is the StudyGrid C++ tutorial?

The StudyGrid C++ tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, classes, the STL, templates, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run c++ syntax examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c++ syntax in this C++ C++ lesson (C++ Syntax).

Is the C++ editor the same as Try Python or Try HTML?

No. Try C++ compiles with g++ at /cpp/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. C++ lessons never open those editors.

Do I need to install a compiler to learn C++?

No. Open a chapter, click Try it in C++, and compile in the browser. You can also download a .cpp file and compile locally with g++.

Where should I start the C++ tutorial?

Start at C++ Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open C++ Examples, then templates, map, and lambdas. Use Next at the bottom of each chapter.

Is the C++ tutorial free?

Yes. The C++ workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.