Denoting Structure: Semicolons, Parentheses, and Braces
In Python, indentation and whitespace indicates where statements end and how structures nest inside one another. In C++, you need to explicitly indicate this using semicolons, parentheses, and braces.
Curly braces – the {
and }
characters – are almost exactly equivalent to Python indentation. You’ll need to use them in if statements, for loops, while loops, and functions. For example:
Python | C++ |
---|---|
|
|
Although indentation alone does not tell the C++ compiler how structures nest inside one another, it’s important to indent things nonetheless to better convey the meaning of your code.
When using curly braces, it’s customary to put each close brace on its own line and aligned with the part of the code it’s closing out. This makes it easier to see how things are nested.
Parentheses – the (
and )
characters – also have the same meaning as in Python. The difference is that, in C++, several structures require the use of parentheses in places where they were optional in Python. For example, the conditions in an if statement and while loop require parentheses:
Python | C++ |
---|---|
|
|
One of the more obvious differences between Python and C++ is the use of semicolons – the ;
character. In Python, the end of a statement is denoted by a newline. In C++, every statement (except for control statements like for
, if
, and while
) must end with a semicolon. For example:
/***** C++ *****/
int number = 137;
callAFunction(arg1, arg2);
However, make sure that you do not put a semicolon after a control statement like for
, if
, or while
. Similarly, do not put a semicolon at the end of a statement beginning with #
.
/***** Bad C++: Do Not Do This! *****/
#include "strlib.h"; // <-- Oops, no semicolon here!
if (myNumber == 137); { // <-- Oops, no semicolon here!
while (myNumber % 2 == 1); { // <-- Oops, no semicolon here!
myNumber /= 2;
}
}
Types
C++ is a typed language, which means that you sometimes need to explicitly say what type something is.
A type is a fundamental kind of value. Examples include int
, string
, char
(single character, not in Python), double
(equivalent of Python float
). You must explicitly state the type when declaring a variable, but not while using it after that. For example:
/***** C++ *****/
int number = 137; // Declare number; type needed
number += 106; // number already declared; do not include type
Function parameters must also have types; also, every function must include a return type. If the function doesn’t return anything, it has return type void
. However, you don’t have to include the types when calling the function.
Python | C++ |
---|---|
|
|
For Loops
In Python, iterating over a range of numbers can be done using the for
… in loop. In C++, the syntax is a bit more involved:
Python | C++ |
---|---|
|
|
Conditionals
The if
and else
keywords work basically the same way in C++ as they do in Python:
Python | C++ |
---|---|
|
|
In C++, there is no elif
keyword. Instead, write out else if
as two words, like this:
Python | C++ |
---|---|
|
|
In Python, you use and
, not
, and or
to combine or modify predicates. While these keywords will technically work in C++, it's not considered good style to use them. Instead, use the (slightly more cryptic, but more standard) symbols
&&
in place ofand
||
in place ofor
!
in place ofnot
For example:
Python | C++ |
---|---|
|
|
In Python, you can chain inequalities together. In C++, you cannot do this, and instead need to build multiple inequalities and use &&
to combine them together:
Python | C++ |
---|---|
|
|
Comments
Python has single-line comments that start with #
. In C++, we use //
instead.
Python | C++ |
---|---|
|
|
C++ also has multiline comments that can be used to describe a dense block of code. They begin with the sequence /*
and end with */
. For aesthetic reasons it’s common to see each line of the comment starting with a star, but this isn’t strictly necessary.
Python | C++ |
---|---|
|
|
Python uses docstrings to document what a function does inside the body of the function. In C++, the convention is to use a multiline comment before the body of the function:
Python | C++ |
---|---|
|
|
Function Prototypes
C++ (for the most part) uses a model called one-pass compilation. This means that the C++ compiler starts at the top of the program, reading downward, and only knows about functions that it’s seen so far. As a result, if you want to call a function that you will eventually define but haven’t yet gotten to writing, you need to include a prototype for that function at the top of the program.
Python | C++ |
---|---|
|
|
Strings and Characters
C++ makes a distinction between strings and characters. A character (denoted by the type char
) is a single glyph you can display on the screen. A string (denoted by the type string
) is a sequence of zero or more characters. Anything in single quotes (e.g. 'a'
) is considered a char
, while anything in doublequotes is considered a string (e.g. "a"
). For example:
Python | C++ |
---|---|
|
|
Many string functions built into Python are not present in C++, but you can get the same functionality by using functions from the "strlib.h" header file.
Python | C++ |
---|---|
|
|
Substrings
Substrings in C++ work differently than in Python. In Python, the notation str[start:end]
gives you a substring starting at position start
and ending at position end
. In C++, the function call str.substr(start, len)
gives you a substring of length len
starting just before position start
. You can also use str.substr(start)
to get a substring starting at position start
and continuing to the end of the string. Negative indices are not allowed.
Python | C++ |
---|---|
|
|